xref: /petsc/src/dm/interface/dm.c (revision 64b0854dba4c6cd69c34e48c8b62323548e7d7b3)
1 #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2 #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3 #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
4 #include <petscdmplex.h>
5 #include <petscdmfield.h>
6 #include <petscsf.h>
7 #include <petscds.h>
8 
9 PetscClassId  DM_CLASSID;
10 PetscClassId  DMLABEL_CLASSID;
11 PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
12 
13 const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
14 
15 static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
16 {
17   PetscFunctionBegin;
18   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19   PetscValidPointer(flg,2);
20   *flg = PETSC_FALSE;
21   PetscFunctionReturn(0);
22 }
23 
24 /*@
25   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
26 
27    If you never  call DMSetType()  it will generate an
28    error when you try to use the vector.
29 
30   Collective on MPI_Comm
31 
32   Input Parameter:
33 . comm - The communicator for the DM object
34 
35   Output Parameter:
36 . dm - The DM object
37 
38   Level: beginner
39 
40 .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
41 @*/
42 PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
43 {
44   DM             v;
45   PetscDS        ds;
46   PetscErrorCode ierr;
47 
48   PetscFunctionBegin;
49   PetscValidPointer(dm,2);
50   *dm = NULL;
51   ierr = DMInitializePackage();CHKERRQ(ierr);
52 
53   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
54 
55   v->setupcalled              = PETSC_FALSE;
56   v->setfromoptionscalled     = PETSC_FALSE;
57   v->ltogmap                  = NULL;
58   v->bs                       = 1;
59   v->coloringtype             = IS_COLORING_GLOBAL;
60   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
61   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
62   v->labels                   = NULL;
63   v->depthLabel               = NULL;
64   v->defaultSection           = NULL;
65   v->defaultGlobalSection     = NULL;
66   v->defaultConstraintSection = NULL;
67   v->defaultConstraintMat     = NULL;
68   v->L                        = NULL;
69   v->maxCell                  = NULL;
70   v->bdtype                   = NULL;
71   v->dimEmbed                 = PETSC_DEFAULT;
72   v->dim                      = PETSC_DETERMINE;
73   {
74     PetscInt i;
75     for (i = 0; i < 10; ++i) {
76       v->nullspaceConstructors[i] = NULL;
77       v->nearnullspaceConstructors[i] = NULL;
78     }
79   }
80   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
81   ierr = DMSetRegionDS(v, NULL, ds);CHKERRQ(ierr);
82   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
83   v->dmBC = NULL;
84   v->coarseMesh = NULL;
85   v->outputSequenceNum = -1;
86   v->outputSequenceVal = 0.0;
87   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
88   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
89   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
90   v->labels->refct = 1;
91 
92   v->ops->hascreateinjection = DMHasCreateInjection_Default;
93 
94   *dm = v;
95   PetscFunctionReturn(0);
96 }
97 
98 /*@
99   DMClone - Creates a DM object with the same topology as the original.
100 
101   Collective on MPI_Comm
102 
103   Input Parameter:
104 . dm - The original DM object
105 
106   Output Parameter:
107 . newdm  - The new DM object
108 
109   Level: beginner
110 
111 .keywords: DM, topology, create
112 @*/
113 PetscErrorCode DMClone(DM dm, DM *newdm)
114 {
115   PetscSF        sf;
116   Vec            coords;
117   void          *ctx;
118   PetscInt       dim, cdim;
119   PetscErrorCode ierr;
120 
121   PetscFunctionBegin;
122   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
123   PetscValidPointer(newdm,2);
124   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
125   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
126   dm->labels->refct++;
127   (*newdm)->labels = dm->labels;
128   (*newdm)->depthLabel = dm->depthLabel;
129   (*newdm)->leveldown  = dm->leveldown;
130   (*newdm)->levelup    = dm->levelup;
131   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
132   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
133   if (dm->ops->clone) {
134     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
135   }
136   (*newdm)->setupcalled = dm->setupcalled;
137   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
138   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
139   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
140   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
141   if (dm->coordinateDM) {
142     DM           ncdm;
143     PetscSection cs;
144     PetscInt     pEnd = -1, pEndMax = -1;
145 
146     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
147     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
148     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
149     if (pEndMax >= 0) {
150       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
151       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
152       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
153       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
154     }
155   }
156   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
157   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
158   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
159   if (coords) {
160     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
161   } else {
162     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
163     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
164   }
165   {
166     PetscBool             isper;
167     const PetscReal      *maxCell, *L;
168     const DMBoundaryType *bd;
169     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
170     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
171   }
172   PetscFunctionReturn(0);
173 }
174 
175 /*@C
176        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
177 
178    Logically Collective on DM
179 
180    Input Parameter:
181 +  da - initial distributed array
182 .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
183 
184    Options Database:
185 .   -dm_vec_type ctype
186 
187    Level: intermediate
188 
189 .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
190 @*/
191 PetscErrorCode  DMSetVecType(DM da,VecType ctype)
192 {
193   PetscErrorCode ierr;
194 
195   PetscFunctionBegin;
196   PetscValidHeaderSpecific(da,DM_CLASSID,1);
197   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
198   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
199   PetscFunctionReturn(0);
200 }
201 
202 /*@C
203        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
204 
205    Logically Collective on DM
206 
207    Input Parameter:
208 .  da - initial distributed array
209 
210    Output Parameter:
211 .  ctype - the vector type
212 
213    Level: intermediate
214 
215 .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
216 @*/
217 PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
218 {
219   PetscFunctionBegin;
220   PetscValidHeaderSpecific(da,DM_CLASSID,1);
221   *ctype = da->vectype;
222   PetscFunctionReturn(0);
223 }
224 
225 /*@
226   VecGetDM - Gets the DM defining the data layout of the vector
227 
228   Not collective
229 
230   Input Parameter:
231 . v - The Vec
232 
233   Output Parameter:
234 . dm - The DM
235 
236   Level: intermediate
237 
238 .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
239 @*/
240 PetscErrorCode VecGetDM(Vec v, DM *dm)
241 {
242   PetscErrorCode ierr;
243 
244   PetscFunctionBegin;
245   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
246   PetscValidPointer(dm,2);
247   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
248   PetscFunctionReturn(0);
249 }
250 
251 /*@
252   VecSetDM - Sets the DM defining the data layout of the vector.
253 
254   Not collective
255 
256   Input Parameters:
257 + v - The Vec
258 - dm - The DM
259 
260   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
261 
262   Level: intermediate
263 
264 .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
265 @*/
266 PetscErrorCode VecSetDM(Vec v, DM dm)
267 {
268   PetscErrorCode ierr;
269 
270   PetscFunctionBegin;
271   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
272   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
273   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
274   PetscFunctionReturn(0);
275 }
276 
277 /*@C
278        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
279 
280    Logically Collective on DM
281 
282    Input Parameters:
283 +  dm - the DM context
284 -  ctype - the matrix type
285 
286    Options Database:
287 .   -dm_is_coloring_type - global or local
288 
289    Level: intermediate
290 
291 .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
292           DMGetISColoringType()
293 @*/
294 PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
295 {
296   PetscFunctionBegin;
297   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
298   dm->coloringtype = ctype;
299   PetscFunctionReturn(0);
300 }
301 
302 /*@C
303        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
304 
305    Logically Collective on DM
306 
307    Input Parameter:
308 .  dm - the DM context
309 
310    Output Parameter:
311 .  ctype - the matrix type
312 
313    Options Database:
314 .   -dm_is_coloring_type - global or local
315 
316    Level: intermediate
317 
318 .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
319           DMGetISColoringType()
320 @*/
321 PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
322 {
323   PetscFunctionBegin;
324   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
325   *ctype = dm->coloringtype;
326   PetscFunctionReturn(0);
327 }
328 
329 /*@C
330        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
331 
332    Logically Collective on DM
333 
334    Input Parameters:
335 +  dm - the DM context
336 -  ctype - the matrix type
337 
338    Options Database:
339 .   -dm_mat_type ctype
340 
341    Level: intermediate
342 
343 .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
344 @*/
345 PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
346 {
347   PetscErrorCode ierr;
348 
349   PetscFunctionBegin;
350   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
351   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
352   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
353   PetscFunctionReturn(0);
354 }
355 
356 /*@C
357        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
358 
359    Logically Collective on DM
360 
361    Input Parameter:
362 .  dm - the DM context
363 
364    Output Parameter:
365 .  ctype - the matrix type
366 
367    Options Database:
368 .   -dm_mat_type ctype
369 
370    Level: intermediate
371 
372 .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
373 @*/
374 PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
375 {
376   PetscFunctionBegin;
377   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
378   *ctype = dm->mattype;
379   PetscFunctionReturn(0);
380 }
381 
382 /*@
383   MatGetDM - Gets the DM defining the data layout of the matrix
384 
385   Not collective
386 
387   Input Parameter:
388 . A - The Mat
389 
390   Output Parameter:
391 . dm - The DM
392 
393   Level: intermediate
394 
395   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
396                   the Mat through a PetscObjectCompose() operation
397 
398 .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
399 @*/
400 PetscErrorCode MatGetDM(Mat A, DM *dm)
401 {
402   PetscErrorCode ierr;
403 
404   PetscFunctionBegin;
405   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
406   PetscValidPointer(dm,2);
407   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
408   PetscFunctionReturn(0);
409 }
410 
411 /*@
412   MatSetDM - Sets the DM defining the data layout of the matrix
413 
414   Not collective
415 
416   Input Parameters:
417 + A - The Mat
418 - dm - The DM
419 
420   Level: intermediate
421 
422   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
423                   the Mat through a PetscObjectCompose() operation
424 
425 
426 .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
427 @*/
428 PetscErrorCode MatSetDM(Mat A, DM dm)
429 {
430   PetscErrorCode ierr;
431 
432   PetscFunctionBegin;
433   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
434   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
435   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
436   PetscFunctionReturn(0);
437 }
438 
439 /*@C
440    DMSetOptionsPrefix - Sets the prefix used for searching for all
441    DM options in the database.
442 
443    Logically Collective on DM
444 
445    Input Parameter:
446 +  da - the DM context
447 -  prefix - the prefix to prepend to all option names
448 
449    Notes:
450    A hyphen (-) must NOT be given at the beginning of the prefix name.
451    The first character of all runtime options is AUTOMATICALLY the hyphen.
452 
453    Level: advanced
454 
455 .keywords: DM, set, options, prefix, database
456 
457 .seealso: DMSetFromOptions()
458 @*/
459 PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
460 {
461   PetscErrorCode ierr;
462 
463   PetscFunctionBegin;
464   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
465   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
466   if (dm->sf) {
467     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
468   }
469   if (dm->defaultSF) {
470     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
471   }
472   PetscFunctionReturn(0);
473 }
474 
475 /*@C
476    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
477    DM options in the database.
478 
479    Logically Collective on DM
480 
481    Input Parameters:
482 +  dm - the DM context
483 -  prefix - the prefix string to prepend to all DM option requests
484 
485    Notes:
486    A hyphen (-) must NOT be given at the beginning of the prefix name.
487    The first character of all runtime options is AUTOMATICALLY the hyphen.
488 
489    Level: advanced
490 
491 .keywords: DM, append, options, prefix, database
492 
493 .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
494 @*/
495 PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
496 {
497   PetscErrorCode ierr;
498 
499   PetscFunctionBegin;
500   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
501   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
502   PetscFunctionReturn(0);
503 }
504 
505 /*@C
506    DMGetOptionsPrefix - Gets the prefix used for searching for all
507    DM options in the database.
508 
509    Not Collective
510 
511    Input Parameters:
512 .  dm - the DM context
513 
514    Output Parameters:
515 .  prefix - pointer to the prefix string used is returned
516 
517    Notes:
518     On the fortran side, the user should pass in a string 'prefix' of
519    sufficient length to hold the prefix.
520 
521    Level: advanced
522 
523 .keywords: DM, set, options, prefix, database
524 
525 .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
526 @*/
527 PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
528 {
529   PetscErrorCode ierr;
530 
531   PetscFunctionBegin;
532   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
533   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
534   PetscFunctionReturn(0);
535 }
536 
537 static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
538 {
539   PetscInt i, refct = ((PetscObject) dm)->refct;
540   DMNamedVecLink nlink;
541   PetscErrorCode ierr;
542 
543   PetscFunctionBegin;
544   *ncrefct = 0;
545   /* count all the circular references of DM and its contained Vecs */
546   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
547     if (dm->localin[i])  refct--;
548     if (dm->globalin[i]) refct--;
549   }
550   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
551   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
552   if (dm->x) {
553     DM obj;
554     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
555     if (obj == dm) refct--;
556   }
557   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
558     refct--;
559     if (recurseCoarse) {
560       PetscInt coarseCount;
561 
562       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
563       refct += coarseCount;
564     }
565   }
566   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
567     refct--;
568     if (recurseFine) {
569       PetscInt fineCount;
570 
571       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
572       refct += fineCount;
573     }
574   }
575   *ncrefct = refct;
576   PetscFunctionReturn(0);
577 }
578 
579 PetscErrorCode DMDestroyLabelLinkList(DM dm)
580 {
581   PetscErrorCode ierr;
582 
583   PetscFunctionBegin;
584   if (!--(dm->labels->refct)) {
585     DMLabelLink next = dm->labels->next;
586 
587     /* destroy the labels */
588     while (next) {
589       DMLabelLink tmp = next->next;
590 
591       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
592       ierr = PetscFree(next);CHKERRQ(ierr);
593       next = tmp;
594     }
595     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
596   }
597   PetscFunctionReturn(0);
598 }
599 
600 /*@
601     DMDestroy - Destroys a vector packer or DM.
602 
603     Collective on DM
604 
605     Input Parameter:
606 .   dm - the DM object to destroy
607 
608     Level: developer
609 
610 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
611 
612 @*/
613 PetscErrorCode  DMDestroy(DM *dm)
614 {
615   PetscInt       i, cnt;
616   DMNamedVecLink nlink,nnext;
617   PetscErrorCode ierr;
618 
619   PetscFunctionBegin;
620   if (!*dm) PetscFunctionReturn(0);
621   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
622 
623   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
624   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
625   --((PetscObject)(*dm))->refct;
626   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
627   /*
628      Need this test because the dm references the vectors that
629      reference the dm, so destroying the dm calls destroy on the
630      vectors that cause another destroy on the dm
631   */
632   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
633   ((PetscObject) (*dm))->refct = 0;
634   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
635     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
636     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
637   }
638   nnext=(*dm)->namedglobal;
639   (*dm)->namedglobal = NULL;
640   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
641     nnext = nlink->next;
642     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
643     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
644     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
645     ierr = PetscFree(nlink);CHKERRQ(ierr);
646   }
647   nnext=(*dm)->namedlocal;
648   (*dm)->namedlocal = NULL;
649   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
650     nnext = nlink->next;
651     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
652     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
653     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
654     ierr = PetscFree(nlink);CHKERRQ(ierr);
655   }
656 
657   /* Destroy the list of hooks */
658   {
659     DMCoarsenHookLink link,next;
660     for (link=(*dm)->coarsenhook; link; link=next) {
661       next = link->next;
662       ierr = PetscFree(link);CHKERRQ(ierr);
663     }
664     (*dm)->coarsenhook = NULL;
665   }
666   {
667     DMRefineHookLink link,next;
668     for (link=(*dm)->refinehook; link; link=next) {
669       next = link->next;
670       ierr = PetscFree(link);CHKERRQ(ierr);
671     }
672     (*dm)->refinehook = NULL;
673   }
674   {
675     DMSubDomainHookLink link,next;
676     for (link=(*dm)->subdomainhook; link; link=next) {
677       next = link->next;
678       ierr = PetscFree(link);CHKERRQ(ierr);
679     }
680     (*dm)->subdomainhook = NULL;
681   }
682   {
683     DMGlobalToLocalHookLink link,next;
684     for (link=(*dm)->gtolhook; link; link=next) {
685       next = link->next;
686       ierr = PetscFree(link);CHKERRQ(ierr);
687     }
688     (*dm)->gtolhook = NULL;
689   }
690   {
691     DMLocalToGlobalHookLink link,next;
692     for (link=(*dm)->ltoghook; link; link=next) {
693       next = link->next;
694       ierr = PetscFree(link);CHKERRQ(ierr);
695     }
696     (*dm)->ltoghook = NULL;
697   }
698   /* Destroy the work arrays */
699   {
700     DMWorkLink link,next;
701     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
702     for (link=(*dm)->workin; link; link=next) {
703       next = link->next;
704       ierr = PetscFree(link->mem);CHKERRQ(ierr);
705       ierr = PetscFree(link);CHKERRQ(ierr);
706     }
707     (*dm)->workin = NULL;
708   }
709   if (!--((*dm)->labels->refct)) {
710     DMLabelLink next = (*dm)->labels->next;
711 
712     /* destroy the labels */
713     while (next) {
714       DMLabelLink tmp = next->next;
715 
716       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
717       ierr = PetscFree(next);CHKERRQ(ierr);
718       next = tmp;
719     }
720     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
721   }
722   ierr = DMClearFields(*dm);CHKERRQ(ierr);
723   {
724     DMBoundary next = (*dm)->boundary;
725     while (next) {
726       DMBoundary b = next;
727 
728       next = b->next;
729       ierr = PetscFree(b);CHKERRQ(ierr);
730     }
731   }
732 
733   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
734   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
735   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
736 
737   if ((*dm)->ctx && (*dm)->ctxdestroy) {
738     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
739   }
740   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
741   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
742   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
743   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
744   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
745   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
746 
747   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
748   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
749   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
750   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
751   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
752   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
753   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
754   if ((*dm)->useNatural) {
755     if ((*dm)->sfNatural) {
756       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
757     }
758     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
759   }
760   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
761     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
762   }
763   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
764   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
765     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
766   }
767   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
768   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
769   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
770   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
771   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
772   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
773 
774   ierr = DMClearDS(*dm);CHKERRQ(ierr);
775   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
776   /* if memory was published with SAWs then destroy it */
777   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
778 
779   if ((*dm)->ops->destroy) {
780     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
781   }
782   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
783   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
784   PetscFunctionReturn(0);
785 }
786 
787 /*@
788     DMSetUp - sets up the data structures inside a DM object
789 
790     Collective on DM
791 
792     Input Parameter:
793 .   dm - the DM object to setup
794 
795     Level: developer
796 
797 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
798 
799 @*/
800 PetscErrorCode  DMSetUp(DM dm)
801 {
802   PetscErrorCode ierr;
803 
804   PetscFunctionBegin;
805   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
806   if (dm->setupcalled) PetscFunctionReturn(0);
807   if (dm->ops->setup) {
808     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
809   }
810   dm->setupcalled = PETSC_TRUE;
811   PetscFunctionReturn(0);
812 }
813 
814 /*@
815     DMSetFromOptions - sets parameters in a DM from the options database
816 
817     Collective on DM
818 
819     Input Parameter:
820 .   dm - the DM object to set options for
821 
822     Options Database:
823 +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
824 .   -dm_vec_type <type>  - type of vector to create inside DM
825 .   -dm_mat_type <type>  - type of matrix to create inside DM
826 -   -dm_is_coloring_type - <global or local>
827 
828     Level: developer
829 
830 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
831 
832 @*/
833 PetscErrorCode DMSetFromOptions(DM dm)
834 {
835   char           typeName[256];
836   PetscBool      flg;
837   PetscErrorCode ierr;
838 
839   PetscFunctionBegin;
840   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
841   dm->setfromoptionscalled = PETSC_TRUE;
842   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
843   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
844   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
845   ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr);
846   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
847   if (flg) {
848     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
849   }
850   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
851   if (flg) {
852     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
853   }
854   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
855   if (dm->ops->setfromoptions) {
856     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
857   }
858   /* process any options handlers added with PetscObjectAddOptionsHandler() */
859   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
860   ierr = PetscOptionsEnd();CHKERRQ(ierr);
861   PetscFunctionReturn(0);
862 }
863 
864 /*@C
865     DMView - Views a DM
866 
867     Collective on DM
868 
869     Input Parameter:
870 +   dm - the DM object to view
871 -   v - the viewer
872 
873     Level: beginner
874 
875 .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
876 
877 @*/
878 PetscErrorCode  DMView(DM dm,PetscViewer v)
879 {
880   PetscErrorCode    ierr;
881   PetscBool         isbinary;
882   PetscMPIInt       size;
883   PetscViewerFormat format;
884 
885   PetscFunctionBegin;
886   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
887   if (!v) {
888     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
889   }
890   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
891   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
892   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
893   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
894   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
895   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
896   if (isbinary) {
897     PetscInt classid = DM_FILE_CLASSID;
898     char     type[256];
899 
900     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
901     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
902     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
903   }
904   if (dm->ops->view) {
905     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
906   }
907   PetscFunctionReturn(0);
908 }
909 
910 /*@
911     DMCreateGlobalVector - Creates a global vector from a DM object
912 
913     Collective on DM
914 
915     Input Parameter:
916 .   dm - the DM object
917 
918     Output Parameter:
919 .   vec - the global vector
920 
921     Level: beginner
922 
923 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
924 
925 @*/
926 PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
927 {
928   PetscErrorCode ierr;
929 
930   PetscFunctionBegin;
931   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
932   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
933   PetscFunctionReturn(0);
934 }
935 
936 /*@
937     DMCreateLocalVector - Creates a local vector from a DM object
938 
939     Not Collective
940 
941     Input Parameter:
942 .   dm - the DM object
943 
944     Output Parameter:
945 .   vec - the local vector
946 
947     Level: beginner
948 
949 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
950 
951 @*/
952 PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
953 {
954   PetscErrorCode ierr;
955 
956   PetscFunctionBegin;
957   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
958   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
959   PetscFunctionReturn(0);
960 }
961 
962 /*@
963    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
964 
965    Collective on DM
966 
967    Input Parameter:
968 .  dm - the DM that provides the mapping
969 
970    Output Parameter:
971 .  ltog - the mapping
972 
973    Level: intermediate
974 
975    Notes:
976    This mapping can then be used by VecSetLocalToGlobalMapping() or
977    MatSetLocalToGlobalMapping().
978 
979 .seealso: DMCreateLocalVector()
980 @*/
981 PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
982 {
983   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
984   PetscErrorCode ierr;
985 
986   PetscFunctionBegin;
987   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
988   PetscValidPointer(ltog,2);
989   if (!dm->ltogmap) {
990     PetscSection section, sectionGlobal;
991 
992     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
993     if (section) {
994       const PetscInt *cdofs;
995       PetscInt       *ltog;
996       PetscInt        pStart, pEnd, n, p, k, l;
997 
998       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
999       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1000       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1001       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
1002       for (p = pStart, l = 0; p < pEnd; ++p) {
1003         PetscInt bdof, cdof, dof, off, c, cind = 0;
1004 
1005         /* Should probably use constrained dofs */
1006         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1007         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1008         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
1009         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
1010         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
1011         bdof = cdof && (dof-cdof) ? 1 : dof;
1012         if (dof) {
1013           if (bs < 0)          {bs = bdof;}
1014           else if (bs != bdof) {bs = 1;}
1015         }
1016         for (c = 0; c < dof; ++c, ++l) {
1017           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1018           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
1019         }
1020       }
1021       /* Must have same blocksize on all procs (some might have no points) */
1022       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
1023       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
1024       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
1025       else                            {bs = bsMinMax[0];}
1026       bs = bs < 0 ? 1 : bs;
1027       /* Must reduce indices by blocksize */
1028       if (bs > 1) {
1029         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1030         n /= bs;
1031       }
1032       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
1033       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
1034     } else {
1035       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1036       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
1037     }
1038   }
1039   *ltog = dm->ltogmap;
1040   PetscFunctionReturn(0);
1041 }
1042 
1043 /*@
1044    DMGetBlockSize - Gets the inherent block size associated with a DM
1045 
1046    Not Collective
1047 
1048    Input Parameter:
1049 .  dm - the DM with block structure
1050 
1051    Output Parameter:
1052 .  bs - the block size, 1 implies no exploitable block structure
1053 
1054    Level: intermediate
1055 
1056 .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
1057 @*/
1058 PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
1059 {
1060   PetscFunctionBegin;
1061   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1062   PetscValidPointer(bs,2);
1063   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
1064   *bs = dm->bs;
1065   PetscFunctionReturn(0);
1066 }
1067 
1068 /*@
1069     DMCreateInterpolation - Gets interpolation matrix between two DM objects
1070 
1071     Collective on DM
1072 
1073     Input Parameter:
1074 +   dm1 - the DM object
1075 -   dm2 - the second, finer DM object
1076 
1077     Output Parameter:
1078 +  mat - the interpolation
1079 -  vec - the scaling (optional)
1080 
1081     Level: developer
1082 
1083     Notes:
1084     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
1085         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1086 
1087         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1088         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
1089 
1090 
1091 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
1092 
1093 @*/
1094 PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
1095 {
1096   PetscErrorCode ierr;
1097 
1098   PetscFunctionBegin;
1099   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1100   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1101   PetscValidPointer(mat,3);
1102   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1103   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
1104   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1105   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
1106   PetscFunctionReturn(0);
1107 }
1108 
1109 /*@
1110     DMCreateRestriction - Gets restriction matrix between two DM objects
1111 
1112     Collective on DM
1113 
1114     Input Parameter:
1115 +   dm1 - the DM object
1116 -   dm2 - the second, finer DM object
1117 
1118     Output Parameter:
1119 .  mat - the restriction
1120 
1121 
1122     Level: developer
1123 
1124     Notes:
1125     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
1126         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1127 
1128 
1129 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
1130 
1131 @*/
1132 PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
1133 {
1134   PetscErrorCode ierr;
1135 
1136   PetscFunctionBegin;
1137   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1138   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1139   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
1140   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
1141   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
1142   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
1143   PetscFunctionReturn(0);
1144 }
1145 
1146 /*@
1147     DMCreateInjection - Gets injection matrix between two DM objects
1148 
1149     Collective on DM
1150 
1151     Input Parameter:
1152 +   dm1 - the DM object
1153 -   dm2 - the second, finer DM object
1154 
1155     Output Parameter:
1156 .   mat - the injection
1157 
1158     Level: developer
1159 
1160    Notes:
1161     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
1162         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
1163 
1164 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
1165 
1166 @*/
1167 PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
1168 {
1169   PetscErrorCode ierr;
1170 
1171   PetscFunctionBegin;
1172   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1173   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1174   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
1175   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
1176   PetscFunctionReturn(0);
1177 }
1178 
1179 /*@
1180   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1181 
1182   Collective on DM
1183 
1184   Input Parameter:
1185 + dm1 - the DM object
1186 - dm2 - the second, finer DM object
1187 
1188   Output Parameter:
1189 . mat - the interpolation
1190 
1191   Level: developer
1192 
1193 .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1194 @*/
1195 PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1196 {
1197   PetscErrorCode ierr;
1198 
1199   PetscFunctionBegin;
1200   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1201   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1202   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1203   PetscFunctionReturn(0);
1204 }
1205 
1206 /*@
1207     DMCreateColoring - Gets coloring for a DM
1208 
1209     Collective on DM
1210 
1211     Input Parameter:
1212 +   dm - the DM object
1213 -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
1214 
1215     Output Parameter:
1216 .   coloring - the coloring
1217 
1218     Level: developer
1219 
1220 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
1221 
1222 @*/
1223 PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
1224 {
1225   PetscErrorCode ierr;
1226 
1227   PetscFunctionBegin;
1228   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1229   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1230   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
1231   PetscFunctionReturn(0);
1232 }
1233 
1234 /*@
1235     DMCreateMatrix - Gets empty Jacobian for a DM
1236 
1237     Collective on DM
1238 
1239     Input Parameter:
1240 .   dm - the DM object
1241 
1242     Output Parameter:
1243 .   mat - the empty Jacobian
1244 
1245     Level: beginner
1246 
1247     Notes:
1248     This properly preallocates the number of nonzeros in the sparse matrix so you
1249        do not need to do it yourself.
1250 
1251        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1252        the nonzero pattern call DMSetMatrixPreallocateOnly()
1253 
1254        For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used
1255        internally by PETSc.
1256 
1257        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1258        the indices for the global numbering for DMDAs which is complicated.
1259 
1260 .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
1261 
1262 @*/
1263 PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
1264 {
1265   PetscErrorCode ierr;
1266 
1267   PetscFunctionBegin;
1268   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1269   ierr = MatInitializePackage();CHKERRQ(ierr);
1270   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1271   PetscValidPointer(mat,3);
1272   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1273   /* Handle nullspace and near nullspace */
1274   if (dm->Nf) {
1275     MatNullSpace nullSpace;
1276     PetscInt     Nf;
1277 
1278     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1279     if (Nf == 1) {
1280       if (dm->nullspaceConstructors[0]) {
1281         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1282         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1283         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1284       }
1285       if (dm->nearnullspaceConstructors[0]) {
1286         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1287         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1288         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1289       }
1290     }
1291   }
1292   PetscFunctionReturn(0);
1293 }
1294 
1295 /*@
1296   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1297     preallocated but the nonzero structure and zero values will not be set.
1298 
1299   Logically Collective on DM
1300 
1301   Input Parameter:
1302 + dm - the DM
1303 - only - PETSC_TRUE if only want preallocation
1304 
1305   Level: developer
1306 .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1307 @*/
1308 PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1309 {
1310   PetscFunctionBegin;
1311   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1312   dm->prealloc_only = only;
1313   PetscFunctionReturn(0);
1314 }
1315 
1316 /*@
1317   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1318     but the array for values will not be allocated.
1319 
1320   Logically Collective on DM
1321 
1322   Input Parameter:
1323 + dm - the DM
1324 - only - PETSC_TRUE if only want matrix stucture
1325 
1326   Level: developer
1327 .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1328 @*/
1329 PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1330 {
1331   PetscFunctionBegin;
1332   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1333   dm->structure_only = only;
1334   PetscFunctionReturn(0);
1335 }
1336 
1337 /*@C
1338   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1339 
1340   Not Collective
1341 
1342   Input Parameters:
1343 + dm - the DM object
1344 . count - The minium size
1345 - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1346 
1347   Output Parameter:
1348 . array - the work array
1349 
1350   Level: developer
1351 
1352 .seealso DMDestroy(), DMCreate()
1353 @*/
1354 PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1355 {
1356   PetscErrorCode ierr;
1357   DMWorkLink     link;
1358   PetscMPIInt    dsize;
1359 
1360   PetscFunctionBegin;
1361   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1362   PetscValidPointer(mem,4);
1363   if (dm->workin) {
1364     link       = dm->workin;
1365     dm->workin = dm->workin->next;
1366   } else {
1367     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1368   }
1369   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
1370   if (((size_t)dsize*count) > link->bytes) {
1371     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1372     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1373     link->bytes = dsize*count;
1374   }
1375   link->next   = dm->workout;
1376   dm->workout  = link;
1377   *(void**)mem = link->mem;
1378   PetscFunctionReturn(0);
1379 }
1380 
1381 /*@C
1382   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1383 
1384   Not Collective
1385 
1386   Input Parameters:
1387 + dm - the DM object
1388 . count - The minium size
1389 - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1390 
1391   Output Parameter:
1392 . array - the work array
1393 
1394   Level: developer
1395 
1396   Developer Notes:
1397     count and dtype are ignored, they are only needed for DMGetWorkArray()
1398 .seealso DMDestroy(), DMCreate()
1399 @*/
1400 PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1401 {
1402   DMWorkLink *p,link;
1403 
1404   PetscFunctionBegin;
1405   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1406   PetscValidPointer(mem,4);
1407   for (p=&dm->workout; (link=*p); p=&link->next) {
1408     if (link->mem == *(void**)mem) {
1409       *p           = link->next;
1410       link->next   = dm->workin;
1411       dm->workin   = link;
1412       *(void**)mem = NULL;
1413       PetscFunctionReturn(0);
1414     }
1415   }
1416   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1417 }
1418 
1419 PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1420 {
1421   PetscFunctionBegin;
1422   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1423   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1424   dm->nullspaceConstructors[field] = nullsp;
1425   PetscFunctionReturn(0);
1426 }
1427 
1428 PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1429 {
1430   PetscFunctionBegin;
1431   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1432   PetscValidPointer(nullsp, 3);
1433   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1434   *nullsp = dm->nullspaceConstructors[field];
1435   PetscFunctionReturn(0);
1436 }
1437 
1438 PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1439 {
1440   PetscFunctionBegin;
1441   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1442   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1443   dm->nearnullspaceConstructors[field] = nullsp;
1444   PetscFunctionReturn(0);
1445 }
1446 
1447 PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1448 {
1449   PetscFunctionBegin;
1450   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1451   PetscValidPointer(nullsp, 3);
1452   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1453   *nullsp = dm->nearnullspaceConstructors[field];
1454   PetscFunctionReturn(0);
1455 }
1456 
1457 /*@C
1458   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
1459 
1460   Not collective
1461 
1462   Input Parameter:
1463 . dm - the DM object
1464 
1465   Output Parameters:
1466 + numFields  - The number of fields (or NULL if not requested)
1467 . fieldNames - The name for each field (or NULL if not requested)
1468 - fields     - The global indices for each field (or NULL if not requested)
1469 
1470   Level: intermediate
1471 
1472   Notes:
1473   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1474   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
1475   PetscFree().
1476 
1477 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
1478 @*/
1479 PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
1480 {
1481   PetscSection   section, sectionGlobal;
1482   PetscErrorCode ierr;
1483 
1484   PetscFunctionBegin;
1485   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1486   if (numFields) {
1487     PetscValidPointer(numFields,2);
1488     *numFields = 0;
1489   }
1490   if (fieldNames) {
1491     PetscValidPointer(fieldNames,3);
1492     *fieldNames = NULL;
1493   }
1494   if (fields) {
1495     PetscValidPointer(fields,4);
1496     *fields = NULL;
1497   }
1498   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1499   if (section) {
1500     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
1501     PetscInt nF, f, pStart, pEnd, p;
1502 
1503     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1504     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1505     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
1506     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1507     for (f = 0; f < nF; ++f) {
1508       fieldSizes[f] = 0;
1509       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
1510     }
1511     for (p = pStart; p < pEnd; ++p) {
1512       PetscInt gdof;
1513 
1514       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
1515       if (gdof > 0) {
1516         for (f = 0; f < nF; ++f) {
1517           PetscInt fdof, fcdof, fpdof;
1518 
1519           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
1520           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
1521           fpdof = fdof-fcdof;
1522           if (fpdof && fpdof != fieldNc[f]) {
1523             /* Layout does not admit a pointwise block size */
1524             fieldNc[f] = 1;
1525           }
1526           fieldSizes[f] += fpdof;
1527         }
1528       }
1529     }
1530     for (f = 0; f < nF; ++f) {
1531       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
1532       fieldSizes[f] = 0;
1533     }
1534     for (p = pStart; p < pEnd; ++p) {
1535       PetscInt gdof, goff;
1536 
1537       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
1538       if (gdof > 0) {
1539         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
1540         for (f = 0; f < nF; ++f) {
1541           PetscInt fdof, fcdof, fc;
1542 
1543           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
1544           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
1545           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
1546             fieldIndices[f][fieldSizes[f]] = goff++;
1547           }
1548         }
1549       }
1550     }
1551     if (numFields) *numFields = nF;
1552     if (fieldNames) {
1553       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
1554       for (f = 0; f < nF; ++f) {
1555         const char *fieldName;
1556 
1557         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1558         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
1559       }
1560     }
1561     if (fields) {
1562       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
1563       for (f = 0; f < nF; ++f) {
1564         PetscInt bs, in[2], out[2];
1565 
1566         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
1567         in[0] = -fieldNc[f];
1568         in[1] = fieldNc[f];
1569         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1570         bs    = (-out[0] == out[1]) ? out[1] : 1;
1571         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
1572       }
1573     }
1574     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
1575   } else if (dm->ops->createfieldis) {
1576     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
1577   }
1578   PetscFunctionReturn(0);
1579 }
1580 
1581 
1582 /*@C
1583   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
1584                           corresponding to different fields: each IS contains the global indices of the dofs of the
1585                           corresponding field. The optional list of DMs define the DM for each subproblem.
1586                           Generalizes DMCreateFieldIS().
1587 
1588   Not collective
1589 
1590   Input Parameter:
1591 . dm - the DM object
1592 
1593   Output Parameters:
1594 + len       - The number of subproblems in the field decomposition (or NULL if not requested)
1595 . namelist  - The name for each field (or NULL if not requested)
1596 . islist    - The global indices for each field (or NULL if not requested)
1597 - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1598 
1599   Level: intermediate
1600 
1601   Notes:
1602   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1603   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1604   and all of the arrays should be freed with PetscFree().
1605 
1606 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1607 @*/
1608 PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1609 {
1610   PetscErrorCode ierr;
1611 
1612   PetscFunctionBegin;
1613   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1614   if (len) {
1615     PetscValidPointer(len,2);
1616     *len = 0;
1617   }
1618   if (namelist) {
1619     PetscValidPointer(namelist,3);
1620     *namelist = 0;
1621   }
1622   if (islist) {
1623     PetscValidPointer(islist,4);
1624     *islist = 0;
1625   }
1626   if (dmlist) {
1627     PetscValidPointer(dmlist,5);
1628     *dmlist = 0;
1629   }
1630   /*
1631    Is it a good idea to apply the following check across all impls?
1632    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1633    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1634    */
1635   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
1636   if (!dm->ops->createfielddecomposition) {
1637     PetscSection section;
1638     PetscInt     numFields, f;
1639 
1640     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1641     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1642     if (section && numFields && dm->ops->createsubdm) {
1643       if (len) *len = numFields;
1644       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
1645       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
1646       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1647       for (f = 0; f < numFields; ++f) {
1648         const char *fieldName;
1649 
1650         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
1651         if (namelist) {
1652           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1653           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1654         }
1655       }
1656     } else {
1657       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1658       /* By default there are no DMs associated with subproblems. */
1659       if (dmlist) *dmlist = NULL;
1660     }
1661   } else {
1662     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
1663   }
1664   PetscFunctionReturn(0);
1665 }
1666 
1667 /*@
1668   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1669                   The fields are defined by DMCreateFieldIS().
1670 
1671   Not collective
1672 
1673   Input Parameters:
1674 + dm        - The DM object
1675 . numFields - The number of fields in this subproblem
1676 - fields    - The field numbers of the selected fields
1677 
1678   Output Parameters:
1679 + is - The global indices for the subproblem
1680 - subdm - The DM for the subproblem
1681 
1682   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
1683 
1684   Level: intermediate
1685 
1686 .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1687 @*/
1688 PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1689 {
1690   PetscErrorCode ierr;
1691 
1692   PetscFunctionBegin;
1693   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1694   PetscValidPointer(fields,3);
1695   if (is) PetscValidPointer(is,4);
1696   if (subdm) PetscValidPointer(subdm,5);
1697   if (dm->ops->createsubdm) {
1698     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1699   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1700   PetscFunctionReturn(0);
1701 }
1702 
1703 /*@C
1704   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
1705 
1706   Not collective
1707 
1708   Input Parameter:
1709 + dms - The DM objects
1710 - len - The number of DMs
1711 
1712   Output Parameters:
1713 + is - The global indices for the subproblem, or NULL
1714 - superdm - The DM for the superproblem
1715 
1716   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
1717 
1718   Level: intermediate
1719 
1720 .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1721 @*/
1722 PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
1723 {
1724   PetscInt       i;
1725   PetscErrorCode ierr;
1726 
1727   PetscFunctionBegin;
1728   PetscValidPointer(dms,1);
1729   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
1730   if (is) PetscValidPointer(is,3);
1731   PetscValidPointer(superdm,4);
1732   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
1733   if (len) {
1734     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1735     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
1736   }
1737   PetscFunctionReturn(0);
1738 }
1739 
1740 
1741 /*@C
1742   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
1743                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
1744                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
1745                           define a nonoverlapping covering, while outer subdomains can overlap.
1746                           The optional list of DMs define the DM for each subproblem.
1747 
1748   Not collective
1749 
1750   Input Parameter:
1751 . dm - the DM object
1752 
1753   Output Parameters:
1754 + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
1755 . namelist    - The name for each subdomain (or NULL if not requested)
1756 . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
1757 . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
1758 - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1759 
1760   Level: intermediate
1761 
1762   Notes:
1763   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1764   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1765   and all of the arrays should be freed with PetscFree().
1766 
1767 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
1768 @*/
1769 PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
1770 {
1771   PetscErrorCode      ierr;
1772   DMSubDomainHookLink link;
1773   PetscInt            i,l;
1774 
1775   PetscFunctionBegin;
1776   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1777   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
1778   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
1779   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
1780   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
1781   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1782   /*
1783    Is it a good idea to apply the following check across all impls?
1784    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1785    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1786    */
1787   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
1788   if (dm->ops->createdomaindecomposition) {
1789     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
1790     /* copy subdomain hooks and context over to the subdomain DMs */
1791     if (dmlist && *dmlist) {
1792       for (i = 0; i < l; i++) {
1793         for (link=dm->subdomainhook; link; link=link->next) {
1794           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1795         }
1796         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1797       }
1798     }
1799     if (len) *len = l;
1800   }
1801   PetscFunctionReturn(0);
1802 }
1803 
1804 
1805 /*@C
1806   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1807 
1808   Not collective
1809 
1810   Input Parameters:
1811 + dm - the DM object
1812 . n  - the number of subdomain scatters
1813 - subdms - the local subdomains
1814 
1815   Output Parameters:
1816 + n     - the number of scatters returned
1817 . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1818 . oscat - scatter from global vector to overlapping global vector entries on subdomain
1819 - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1820 
1821   Notes:
1822     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1823   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1824   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1825   solution and residual data.
1826 
1827   Level: developer
1828 
1829 .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1830 @*/
1831 PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1832 {
1833   PetscErrorCode ierr;
1834 
1835   PetscFunctionBegin;
1836   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1837   PetscValidPointer(subdms,3);
1838   if (dm->ops->createddscatters) {
1839     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1840   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1841   PetscFunctionReturn(0);
1842 }
1843 
1844 /*@
1845   DMRefine - Refines a DM object
1846 
1847   Collective on DM
1848 
1849   Input Parameter:
1850 + dm   - the DM object
1851 - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
1852 
1853   Output Parameter:
1854 . dmf - the refined DM, or NULL
1855 
1856   Note: If no refinement was done, the return value is NULL
1857 
1858   Level: developer
1859 
1860 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1861 @*/
1862 PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
1863 {
1864   PetscErrorCode   ierr;
1865   DMRefineHookLink link;
1866 
1867   PetscFunctionBegin;
1868   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1869   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1870   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
1871   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
1872   if (*dmf) {
1873     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
1874 
1875     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
1876 
1877     (*dmf)->ctx       = dm->ctx;
1878     (*dmf)->leveldown = dm->leveldown;
1879     (*dmf)->levelup   = dm->levelup + 1;
1880 
1881     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1882     for (link=dm->refinehook; link; link=link->next) {
1883       if (link->refinehook) {
1884         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
1885       }
1886     }
1887   }
1888   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1889   PetscFunctionReturn(0);
1890 }
1891 
1892 /*@C
1893    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1894 
1895    Logically Collective
1896 
1897    Input Arguments:
1898 +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1899 .  refinehook - function to run when setting up a coarser level
1900 .  interphook - function to run to update data on finer levels (once per SNESSolve())
1901 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1902 
1903    Calling sequence of refinehook:
1904 $    refinehook(DM coarse,DM fine,void *ctx);
1905 
1906 +  coarse - coarse level DM
1907 .  fine - fine level DM to interpolate problem to
1908 -  ctx - optional user-defined function context
1909 
1910    Calling sequence for interphook:
1911 $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1912 
1913 +  coarse - coarse level DM
1914 .  interp - matrix interpolating a coarse-level solution to the finer grid
1915 .  fine - fine level DM to update
1916 -  ctx - optional user-defined function context
1917 
1918    Level: advanced
1919 
1920    Notes:
1921    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1922 
1923    If this function is called multiple times, the hooks will be run in the order they are added.
1924 
1925    This function is currently not available from Fortran.
1926 
1927 .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1928 @*/
1929 PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1930 {
1931   PetscErrorCode   ierr;
1932   DMRefineHookLink link,*p;
1933 
1934   PetscFunctionBegin;
1935   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1936   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
1937     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
1938   }
1939   ierr             = PetscNew(&link);CHKERRQ(ierr);
1940   link->refinehook = refinehook;
1941   link->interphook = interphook;
1942   link->ctx        = ctx;
1943   link->next       = NULL;
1944   *p               = link;
1945   PetscFunctionReturn(0);
1946 }
1947 
1948 /*@C
1949    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
1950 
1951    Logically Collective
1952 
1953    Input Arguments:
1954 +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1955 .  refinehook - function to run when setting up a coarser level
1956 .  interphook - function to run to update data on finer levels (once per SNESSolve())
1957 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1958 
1959    Level: advanced
1960 
1961    Notes:
1962    This function does nothing if the hook is not in the list.
1963 
1964    This function is currently not available from Fortran.
1965 
1966 .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1967 @*/
1968 PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1969 {
1970   PetscErrorCode   ierr;
1971   DMRefineHookLink link,*p;
1972 
1973   PetscFunctionBegin;
1974   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1975   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
1976     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
1977       link = *p;
1978       *p = link->next;
1979       ierr = PetscFree(link);CHKERRQ(ierr);
1980       break;
1981     }
1982   }
1983   PetscFunctionReturn(0);
1984 }
1985 
1986 /*@
1987    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1988 
1989    Collective if any hooks are
1990 
1991    Input Arguments:
1992 +  coarse - coarser DM to use as a base
1993 .  interp - interpolation matrix, apply using MatInterpolate()
1994 -  fine - finer DM to update
1995 
1996    Level: developer
1997 
1998 .seealso: DMRefineHookAdd(), MatInterpolate()
1999 @*/
2000 PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2001 {
2002   PetscErrorCode   ierr;
2003   DMRefineHookLink link;
2004 
2005   PetscFunctionBegin;
2006   for (link=fine->refinehook; link; link=link->next) {
2007     if (link->interphook) {
2008       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
2009     }
2010   }
2011   PetscFunctionReturn(0);
2012 }
2013 
2014 /*@
2015     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2016 
2017     Not Collective
2018 
2019     Input Parameter:
2020 .   dm - the DM object
2021 
2022     Output Parameter:
2023 .   level - number of refinements
2024 
2025     Level: developer
2026 
2027 .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2028 
2029 @*/
2030 PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2031 {
2032   PetscFunctionBegin;
2033   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2034   *level = dm->levelup;
2035   PetscFunctionReturn(0);
2036 }
2037 
2038 /*@
2039     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2040 
2041     Not Collective
2042 
2043     Input Parameter:
2044 +   dm - the DM object
2045 -   level - number of refinements
2046 
2047     Level: advanced
2048 
2049     Notes:
2050     This value is used by PCMG to determine how many multigrid levels to use
2051 
2052 .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2053 
2054 @*/
2055 PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2056 {
2057   PetscFunctionBegin;
2058   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2059   dm->levelup = level;
2060   PetscFunctionReturn(0);
2061 }
2062 
2063 /*@C
2064    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2065 
2066    Logically Collective
2067 
2068    Input Arguments:
2069 +  dm - the DM
2070 .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2071 .  endhook - function to run after DMGlobalToLocalEnd() has completed
2072 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2073 
2074    Calling sequence for beginhook:
2075 $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2076 
2077 +  dm - global DM
2078 .  g - global vector
2079 .  mode - mode
2080 .  l - local vector
2081 -  ctx - optional user-defined function context
2082 
2083 
2084    Calling sequence for endhook:
2085 $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2086 
2087 +  global - global DM
2088 -  ctx - optional user-defined function context
2089 
2090    Level: advanced
2091 
2092 .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2093 @*/
2094 PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2095 {
2096   PetscErrorCode          ierr;
2097   DMGlobalToLocalHookLink link,*p;
2098 
2099   PetscFunctionBegin;
2100   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2101   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2102   ierr            = PetscNew(&link);CHKERRQ(ierr);
2103   link->beginhook = beginhook;
2104   link->endhook   = endhook;
2105   link->ctx       = ctx;
2106   link->next      = NULL;
2107   *p              = link;
2108   PetscFunctionReturn(0);
2109 }
2110 
2111 static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
2112 {
2113   Mat cMat;
2114   Vec cVec;
2115   PetscSection section, cSec;
2116   PetscInt pStart, pEnd, p, dof;
2117   PetscErrorCode ierr;
2118 
2119   PetscFunctionBegin;
2120   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2121   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
2122   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
2123     PetscInt nRows;
2124 
2125     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
2126     if (nRows <= 0) PetscFunctionReturn(0);
2127     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
2128     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
2129     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
2130     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
2131     for (p = pStart; p < pEnd; p++) {
2132       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
2133       if (dof) {
2134         PetscScalar *vals;
2135         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
2136         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
2137       }
2138     }
2139     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
2140   }
2141   PetscFunctionReturn(0);
2142 }
2143 
2144 /*@
2145     DMGlobalToLocal - update local vectors from global vector
2146 
2147     Neighbor-wise Collective on DM
2148 
2149     Input Parameters:
2150 +   dm - the DM object
2151 .   g - the global vector
2152 .   mode - INSERT_VALUES or ADD_VALUES
2153 -   l - the local vector
2154 
2155     Notes:
2156     The communication involved in this update can be overlapped with computation by using
2157     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
2158 
2159     Level: beginner
2160 
2161 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
2162 
2163 @*/
2164 PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
2165 {
2166   PetscErrorCode ierr;
2167 
2168   PetscFunctionBegin;
2169   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
2170   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
2171   PetscFunctionReturn(0);
2172 }
2173 
2174 /*@
2175     DMGlobalToLocalBegin - Begins updating local vectors from global vector
2176 
2177     Neighbor-wise Collective on DM
2178 
2179     Input Parameters:
2180 +   dm - the DM object
2181 .   g - the global vector
2182 .   mode - INSERT_VALUES or ADD_VALUES
2183 -   l - the local vector
2184 
2185     Level: intermediate
2186 
2187 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
2188 
2189 @*/
2190 PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2191 {
2192   PetscSF                 sf;
2193   PetscErrorCode          ierr;
2194   DMGlobalToLocalHookLink link;
2195 
2196   PetscFunctionBegin;
2197   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2198   for (link=dm->gtolhook; link; link=link->next) {
2199     if (link->beginhook) {
2200       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
2201     }
2202   }
2203   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2204   if (sf) {
2205     const PetscScalar *gArray;
2206     PetscScalar       *lArray;
2207 
2208     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2209     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2210     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
2211     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2212     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2213     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2214   } else {
2215     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2216     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2217   }
2218   PetscFunctionReturn(0);
2219 }
2220 
2221 /*@
2222     DMGlobalToLocalEnd - Ends updating local vectors from global vector
2223 
2224     Neighbor-wise Collective on DM
2225 
2226     Input Parameters:
2227 +   dm - the DM object
2228 .   g - the global vector
2229 .   mode - INSERT_VALUES or ADD_VALUES
2230 -   l - the local vector
2231 
2232     Level: intermediate
2233 
2234 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
2235 
2236 @*/
2237 PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2238 {
2239   PetscSF                 sf;
2240   PetscErrorCode          ierr;
2241   const PetscScalar      *gArray;
2242   PetscScalar            *lArray;
2243   DMGlobalToLocalHookLink link;
2244 
2245   PetscFunctionBegin;
2246   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2247   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2248   if (sf) {
2249     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2250 
2251     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2252     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
2253     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2254     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2255     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2256   } else {
2257     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2258     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2259   }
2260   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2261   for (link=dm->gtolhook; link; link=link->next) {
2262     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2263   }
2264   PetscFunctionReturn(0);
2265 }
2266 
2267 /*@C
2268    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2269 
2270    Logically Collective
2271 
2272    Input Arguments:
2273 +  dm - the DM
2274 .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2275 .  endhook - function to run after DMLocalToGlobalEnd() has completed
2276 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2277 
2278    Calling sequence for beginhook:
2279 $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2280 
2281 +  dm - global DM
2282 .  l - local vector
2283 .  mode - mode
2284 .  g - global vector
2285 -  ctx - optional user-defined function context
2286 
2287 
2288    Calling sequence for endhook:
2289 $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2290 
2291 +  global - global DM
2292 .  l - local vector
2293 .  mode - mode
2294 .  g - global vector
2295 -  ctx - optional user-defined function context
2296 
2297    Level: advanced
2298 
2299 .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2300 @*/
2301 PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2302 {
2303   PetscErrorCode          ierr;
2304   DMLocalToGlobalHookLink link,*p;
2305 
2306   PetscFunctionBegin;
2307   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2308   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2309   ierr            = PetscNew(&link);CHKERRQ(ierr);
2310   link->beginhook = beginhook;
2311   link->endhook   = endhook;
2312   link->ctx       = ctx;
2313   link->next      = NULL;
2314   *p              = link;
2315   PetscFunctionReturn(0);
2316 }
2317 
2318 static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
2319 {
2320   Mat cMat;
2321   Vec cVec;
2322   PetscSection section, cSec;
2323   PetscInt pStart, pEnd, p, dof;
2324   PetscErrorCode ierr;
2325 
2326   PetscFunctionBegin;
2327   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2328   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
2329   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
2330     PetscInt nRows;
2331 
2332     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
2333     if (nRows <= 0) PetscFunctionReturn(0);
2334     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
2335     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
2336     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
2337     for (p = pStart; p < pEnd; p++) {
2338       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
2339       if (dof) {
2340         PetscInt d;
2341         PetscScalar *vals;
2342         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
2343         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
2344         /* for this to be the true transpose, we have to zero the values that
2345          * we just extracted */
2346         for (d = 0; d < dof; d++) {
2347           vals[d] = 0.;
2348         }
2349       }
2350     }
2351     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
2352     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
2353   }
2354   PetscFunctionReturn(0);
2355 }
2356 /*@
2357     DMLocalToGlobal - updates global vectors from local vectors
2358 
2359     Neighbor-wise Collective on DM
2360 
2361     Input Parameters:
2362 +   dm - the DM object
2363 .   l - the local vector
2364 .   mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that base point.
2365 -   g - the global vector
2366 
2367     Notes:
2368     The communication involved in this update can be overlapped with computation by using
2369     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
2370 
2371     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
2372            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
2373 
2374     Level: beginner
2375 
2376 .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
2377 
2378 @*/
2379 PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
2380 {
2381   PetscErrorCode ierr;
2382 
2383   PetscFunctionBegin;
2384   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
2385   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
2386   PetscFunctionReturn(0);
2387 }
2388 
2389 /*@
2390     DMLocalToGlobalBegin - begins updating global vectors from local vectors
2391 
2392     Neighbor-wise Collective on DM
2393 
2394     Input Parameters:
2395 +   dm - the DM object
2396 .   l - the local vector
2397 .   mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that base point.
2398 -   g - the global vector
2399 
2400     Notes:
2401     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
2402            INSERT_VALUES is not supported for DMDA, in that case simply compute the values directly into a global vector instead of a local one.
2403 
2404     Level: intermediate
2405 
2406 .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
2407 
2408 @*/
2409 PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
2410 {
2411   PetscSF                 sf;
2412   PetscSection            s, gs;
2413   DMLocalToGlobalHookLink link;
2414   const PetscScalar      *lArray;
2415   PetscScalar            *gArray;
2416   PetscBool               isInsert;
2417   PetscErrorCode          ierr;
2418 
2419   PetscFunctionBegin;
2420   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2421   for (link=dm->ltoghook; link; link=link->next) {
2422     if (link->beginhook) {
2423       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2424     }
2425   }
2426   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
2427   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2428   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
2429   switch (mode) {
2430   case INSERT_VALUES:
2431   case INSERT_ALL_VALUES:
2432   case INSERT_BC_VALUES:
2433     isInsert = PETSC_TRUE; break;
2434   case ADD_VALUES:
2435   case ADD_ALL_VALUES:
2436   case ADD_BC_VALUES:
2437     isInsert = PETSC_FALSE; break;
2438   default:
2439     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2440   }
2441   if (sf && !isInsert) {
2442     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2443     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2444     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2445     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2446     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2447   } else if (s && isInsert) {
2448     PetscInt gStart, pStart, pEnd, p;
2449 
2450     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
2451     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2452     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2453     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2454     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2455     for (p = pStart; p < pEnd; ++p) {
2456       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
2457 
2458       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
2459       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
2460       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2461       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
2462       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
2463       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2464       /* Ignore off-process data and points with no global data */
2465       if (!gdof || goff < 0) continue;
2466       if (dof != gdof) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
2467       /* If no constraints are enforced in the global vector */
2468       if (!gcdof) {
2469         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2470       /* If constraints are enforced in the global vector */
2471       } else if (cdof == gcdof) {
2472         const PetscInt *cdofs;
2473         PetscInt        cind = 0;
2474 
2475         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2476         for (d = 0, e = 0; d < dof; ++d) {
2477           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2478           gArray[goff-gStart+e++] = lArray[off+d];
2479         }
2480       } else SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
2481     }
2482     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2483     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2484   } else {
2485     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
2486   }
2487   PetscFunctionReturn(0);
2488 }
2489 
2490 /*@
2491     DMLocalToGlobalEnd - updates global vectors from local vectors
2492 
2493     Neighbor-wise Collective on DM
2494 
2495     Input Parameters:
2496 +   dm - the DM object
2497 .   l - the local vector
2498 .   mode - INSERT_VALUES or ADD_VALUES
2499 -   g - the global vector
2500 
2501     Level: intermediate
2502 
2503 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
2504 
2505 @*/
2506 PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
2507 {
2508   PetscSF                 sf;
2509   PetscSection            s;
2510   DMLocalToGlobalHookLink link;
2511   PetscBool               isInsert;
2512   PetscErrorCode          ierr;
2513 
2514   PetscFunctionBegin;
2515   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2516   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2517   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
2518   switch (mode) {
2519   case INSERT_VALUES:
2520   case INSERT_ALL_VALUES:
2521     isInsert = PETSC_TRUE; break;
2522   case ADD_VALUES:
2523   case ADD_ALL_VALUES:
2524     isInsert = PETSC_FALSE; break;
2525   default:
2526     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2527   }
2528   if (sf && !isInsert) {
2529     const PetscScalar *lArray;
2530     PetscScalar       *gArray;
2531 
2532     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2533     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2534     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2535     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2536     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2537   } else if (s && isInsert) {
2538   } else {
2539     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
2540   }
2541   for (link=dm->ltoghook; link; link=link->next) {
2542     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2543   }
2544   PetscFunctionReturn(0);
2545 }
2546 
2547 /*@
2548    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2549    that contain irrelevant values) to another local vector where the ghost
2550    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2551 
2552    Neighbor-wise Collective on DM and Vec
2553 
2554    Input Parameters:
2555 +  dm - the DM object
2556 .  g - the original local vector
2557 -  mode - one of INSERT_VALUES or ADD_VALUES
2558 
2559    Output Parameter:
2560 .  l  - the local vector with correct ghost values
2561 
2562    Level: intermediate
2563 
2564    Notes:
2565    The local vectors used here need not be the same as those
2566    obtained from DMCreateLocalVector(), BUT they
2567    must have the same parallel data layout; they could, for example, be
2568    obtained with VecDuplicate() from the DM originating vectors.
2569 
2570 .keywords: DM, local-to-local, begin
2571 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2572 
2573 @*/
2574 PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2575 {
2576   PetscErrorCode          ierr;
2577 
2578   PetscFunctionBegin;
2579   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2580   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2581   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2582   PetscFunctionReturn(0);
2583 }
2584 
2585 /*@
2586    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2587    that contain irrelevant values) to another local vector where the ghost
2588    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2589 
2590    Neighbor-wise Collective on DM and Vec
2591 
2592    Input Parameters:
2593 +  da - the DM object
2594 .  g - the original local vector
2595 -  mode - one of INSERT_VALUES or ADD_VALUES
2596 
2597    Output Parameter:
2598 .  l  - the local vector with correct ghost values
2599 
2600    Level: intermediate
2601 
2602    Notes:
2603    The local vectors used here need not be the same as those
2604    obtained from DMCreateLocalVector(), BUT they
2605    must have the same parallel data layout; they could, for example, be
2606    obtained with VecDuplicate() from the DM originating vectors.
2607 
2608 .keywords: DM, local-to-local, end
2609 .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2610 
2611 @*/
2612 PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2613 {
2614   PetscErrorCode          ierr;
2615 
2616   PetscFunctionBegin;
2617   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2618   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2619   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2620   PetscFunctionReturn(0);
2621 }
2622 
2623 
2624 /*@
2625     DMCoarsen - Coarsens a DM object
2626 
2627     Collective on DM
2628 
2629     Input Parameter:
2630 +   dm - the DM object
2631 -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
2632 
2633     Output Parameter:
2634 .   dmc - the coarsened DM
2635 
2636     Level: developer
2637 
2638 .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2639 
2640 @*/
2641 PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
2642 {
2643   PetscErrorCode    ierr;
2644   DMCoarsenHookLink link;
2645 
2646   PetscFunctionBegin;
2647   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2648   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
2649   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2650   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2651   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2652   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
2653   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
2654   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2655   (*dmc)->ctx               = dm->ctx;
2656   (*dmc)->levelup           = dm->levelup;
2657   (*dmc)->leveldown         = dm->leveldown + 1;
2658   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2659   for (link=dm->coarsenhook; link; link=link->next) {
2660     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2661   }
2662   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2663   PetscFunctionReturn(0);
2664 }
2665 
2666 /*@C
2667    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2668 
2669    Logically Collective
2670 
2671    Input Arguments:
2672 +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2673 .  coarsenhook - function to run when setting up a coarser level
2674 .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2675 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2676 
2677    Calling sequence of coarsenhook:
2678 $    coarsenhook(DM fine,DM coarse,void *ctx);
2679 
2680 +  fine - fine level DM
2681 .  coarse - coarse level DM to restrict problem to
2682 -  ctx - optional user-defined function context
2683 
2684    Calling sequence for restricthook:
2685 $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2686 
2687 +  fine - fine level DM
2688 .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2689 .  rscale - scaling vector for restriction
2690 .  inject - matrix restricting by injection
2691 .  coarse - coarse level DM to update
2692 -  ctx - optional user-defined function context
2693 
2694    Level: advanced
2695 
2696    Notes:
2697    This function is only needed if auxiliary data needs to be set up on coarse grids.
2698 
2699    If this function is called multiple times, the hooks will be run in the order they are added.
2700 
2701    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2702    extract the finest level information from its context (instead of from the SNES).
2703 
2704    This function is currently not available from Fortran.
2705 
2706 .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2707 @*/
2708 PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2709 {
2710   PetscErrorCode    ierr;
2711   DMCoarsenHookLink link,*p;
2712 
2713   PetscFunctionBegin;
2714   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2715   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2716     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2717   }
2718   ierr               = PetscNew(&link);CHKERRQ(ierr);
2719   link->coarsenhook  = coarsenhook;
2720   link->restricthook = restricthook;
2721   link->ctx          = ctx;
2722   link->next         = NULL;
2723   *p                 = link;
2724   PetscFunctionReturn(0);
2725 }
2726 
2727 /*@C
2728    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2729 
2730    Logically Collective
2731 
2732    Input Arguments:
2733 +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2734 .  coarsenhook - function to run when setting up a coarser level
2735 .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2736 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2737 
2738    Level: advanced
2739 
2740    Notes:
2741    This function does nothing if the hook is not in the list.
2742 
2743    This function is currently not available from Fortran.
2744 
2745 .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2746 @*/
2747 PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2748 {
2749   PetscErrorCode    ierr;
2750   DMCoarsenHookLink link,*p;
2751 
2752   PetscFunctionBegin;
2753   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2754   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2755     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2756       link = *p;
2757       *p = link->next;
2758       ierr = PetscFree(link);CHKERRQ(ierr);
2759       break;
2760     }
2761   }
2762   PetscFunctionReturn(0);
2763 }
2764 
2765 
2766 /*@
2767    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2768 
2769    Collective if any hooks are
2770 
2771    Input Arguments:
2772 +  fine - finer DM to use as a base
2773 .  restrct - restriction matrix, apply using MatRestrict()
2774 .  rscale - scaling vector for restriction
2775 .  inject - injection matrix, also use MatRestrict()
2776 -  coarse - coarser DM to update
2777 
2778    Level: developer
2779 
2780 .seealso: DMCoarsenHookAdd(), MatRestrict()
2781 @*/
2782 PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2783 {
2784   PetscErrorCode    ierr;
2785   DMCoarsenHookLink link;
2786 
2787   PetscFunctionBegin;
2788   for (link=fine->coarsenhook; link; link=link->next) {
2789     if (link->restricthook) {
2790       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
2791     }
2792   }
2793   PetscFunctionReturn(0);
2794 }
2795 
2796 /*@C
2797    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
2798 
2799    Logically Collective
2800 
2801    Input Arguments:
2802 +  global - global DM
2803 .  ddhook - function to run to pass data to the decomposition DM upon its creation
2804 .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2805 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2806 
2807 
2808    Calling sequence for ddhook:
2809 $    ddhook(DM global,DM block,void *ctx)
2810 
2811 +  global - global DM
2812 .  block  - block DM
2813 -  ctx - optional user-defined function context
2814 
2815    Calling sequence for restricthook:
2816 $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
2817 
2818 +  global - global DM
2819 .  out    - scatter to the outer (with ghost and overlap points) block vector
2820 .  in     - scatter to block vector values only owned locally
2821 .  block  - block DM
2822 -  ctx - optional user-defined function context
2823 
2824    Level: advanced
2825 
2826    Notes:
2827    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
2828 
2829    If this function is called multiple times, the hooks will be run in the order they are added.
2830 
2831    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2832    extract the global information from its context (instead of from the SNES).
2833 
2834    This function is currently not available from Fortran.
2835 
2836 .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2837 @*/
2838 PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2839 {
2840   PetscErrorCode      ierr;
2841   DMSubDomainHookLink link,*p;
2842 
2843   PetscFunctionBegin;
2844   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2845   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2846     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2847   }
2848   ierr               = PetscNew(&link);CHKERRQ(ierr);
2849   link->restricthook = restricthook;
2850   link->ddhook       = ddhook;
2851   link->ctx          = ctx;
2852   link->next         = NULL;
2853   *p                 = link;
2854   PetscFunctionReturn(0);
2855 }
2856 
2857 /*@C
2858    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2859 
2860    Logically Collective
2861 
2862    Input Arguments:
2863 +  global - global DM
2864 .  ddhook - function to run to pass data to the decomposition DM upon its creation
2865 .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2866 -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2867 
2868    Level: advanced
2869 
2870    Notes:
2871 
2872    This function is currently not available from Fortran.
2873 
2874 .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2875 @*/
2876 PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2877 {
2878   PetscErrorCode      ierr;
2879   DMSubDomainHookLink link,*p;
2880 
2881   PetscFunctionBegin;
2882   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2883   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2884     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2885       link = *p;
2886       *p = link->next;
2887       ierr = PetscFree(link);CHKERRQ(ierr);
2888       break;
2889     }
2890   }
2891   PetscFunctionReturn(0);
2892 }
2893 
2894 /*@
2895    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
2896 
2897    Collective if any hooks are
2898 
2899    Input Arguments:
2900 +  fine - finer DM to use as a base
2901 .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2902 .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
2903 -  coarse - coarer DM to update
2904 
2905    Level: developer
2906 
2907 .seealso: DMCoarsenHookAdd(), MatRestrict()
2908 @*/
2909 PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
2910 {
2911   PetscErrorCode      ierr;
2912   DMSubDomainHookLink link;
2913 
2914   PetscFunctionBegin;
2915   for (link=global->subdomainhook; link; link=link->next) {
2916     if (link->restricthook) {
2917       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
2918     }
2919   }
2920   PetscFunctionReturn(0);
2921 }
2922 
2923 /*@
2924     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
2925 
2926     Not Collective
2927 
2928     Input Parameter:
2929 .   dm - the DM object
2930 
2931     Output Parameter:
2932 .   level - number of coarsenings
2933 
2934     Level: developer
2935 
2936 .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2937 
2938 @*/
2939 PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
2940 {
2941   PetscFunctionBegin;
2942   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2943   *level = dm->leveldown;
2944   PetscFunctionReturn(0);
2945 }
2946 
2947 /*@
2948     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
2949 
2950     Not Collective
2951 
2952     Input Parameters:
2953 +   dm - the DM object
2954 -   level - number of coarsenings
2955 
2956     Level: developer
2957 
2958 .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2959 @*/
2960 PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
2961 {
2962   PetscFunctionBegin;
2963   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2964   dm->leveldown = level;
2965   PetscFunctionReturn(0);
2966 }
2967 
2968 
2969 
2970 /*@C
2971     DMRefineHierarchy - Refines a DM object, all levels at once
2972 
2973     Collective on DM
2974 
2975     Input Parameter:
2976 +   dm - the DM object
2977 -   nlevels - the number of levels of refinement
2978 
2979     Output Parameter:
2980 .   dmf - the refined DM hierarchy
2981 
2982     Level: developer
2983 
2984 .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2985 
2986 @*/
2987 PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
2988 {
2989   PetscErrorCode ierr;
2990 
2991   PetscFunctionBegin;
2992   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2993   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
2994   if (nlevels == 0) PetscFunctionReturn(0);
2995   if (dm->ops->refinehierarchy) {
2996     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
2997   } else if (dm->ops->refine) {
2998     PetscInt i;
2999 
3000     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
3001     for (i=1; i<nlevels; i++) {
3002       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
3003     }
3004   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
3005   PetscFunctionReturn(0);
3006 }
3007 
3008 /*@C
3009     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
3010 
3011     Collective on DM
3012 
3013     Input Parameter:
3014 +   dm - the DM object
3015 -   nlevels - the number of levels of coarsening
3016 
3017     Output Parameter:
3018 .   dmc - the coarsened DM hierarchy
3019 
3020     Level: developer
3021 
3022 .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
3023 
3024 @*/
3025 PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
3026 {
3027   PetscErrorCode ierr;
3028 
3029   PetscFunctionBegin;
3030   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3031   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
3032   if (nlevels == 0) PetscFunctionReturn(0);
3033   PetscValidPointer(dmc,3);
3034   if (dm->ops->coarsenhierarchy) {
3035     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
3036   } else if (dm->ops->coarsen) {
3037     PetscInt i;
3038 
3039     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
3040     for (i=1; i<nlevels; i++) {
3041       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
3042     }
3043   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
3044   PetscFunctionReturn(0);
3045 }
3046 
3047 /*@
3048    DMCreateAggregates - Gets the aggregates that map between
3049    grids associated with two DMs.
3050 
3051    Collective on DM
3052 
3053    Input Parameters:
3054 +  dmc - the coarse grid DM
3055 -  dmf - the fine grid DM
3056 
3057    Output Parameters:
3058 .  rest - the restriction matrix (transpose of the projection matrix)
3059 
3060    Level: intermediate
3061 
3062 .keywords: interpolation, restriction, multigrid
3063 
3064 .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
3065 @*/
3066 PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
3067 {
3068   PetscErrorCode ierr;
3069 
3070   PetscFunctionBegin;
3071   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3072   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
3073   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
3074   PetscFunctionReturn(0);
3075 }
3076 
3077 /*@C
3078     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
3079 
3080     Not Collective
3081 
3082     Input Parameters:
3083 +   dm - the DM object
3084 -   destroy - the destroy function
3085 
3086     Level: intermediate
3087 
3088 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3089 
3090 @*/
3091 PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
3092 {
3093   PetscFunctionBegin;
3094   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3095   dm->ctxdestroy = destroy;
3096   PetscFunctionReturn(0);
3097 }
3098 
3099 /*@
3100     DMSetApplicationContext - Set a user context into a DM object
3101 
3102     Not Collective
3103 
3104     Input Parameters:
3105 +   dm - the DM object
3106 -   ctx - the user context
3107 
3108     Level: intermediate
3109 
3110 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3111 
3112 @*/
3113 PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
3114 {
3115   PetscFunctionBegin;
3116   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3117   dm->ctx = ctx;
3118   PetscFunctionReturn(0);
3119 }
3120 
3121 /*@
3122     DMGetApplicationContext - Gets a user context from a DM object
3123 
3124     Not Collective
3125 
3126     Input Parameter:
3127 .   dm - the DM object
3128 
3129     Output Parameter:
3130 .   ctx - the user context
3131 
3132     Level: intermediate
3133 
3134 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3135 
3136 @*/
3137 PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
3138 {
3139   PetscFunctionBegin;
3140   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3141   *(void**)ctx = dm->ctx;
3142   PetscFunctionReturn(0);
3143 }
3144 
3145 /*@C
3146     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
3147 
3148     Logically Collective on DM
3149 
3150     Input Parameter:
3151 +   dm - the DM object
3152 -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
3153 
3154     Level: intermediate
3155 
3156 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
3157          DMSetJacobian()
3158 
3159 @*/
3160 PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
3161 {
3162   PetscFunctionBegin;
3163   dm->ops->computevariablebounds = f;
3164   PetscFunctionReturn(0);
3165 }
3166 
3167 /*@
3168     DMHasVariableBounds - does the DM object have a variable bounds function?
3169 
3170     Not Collective
3171 
3172     Input Parameter:
3173 .   dm - the DM object to destroy
3174 
3175     Output Parameter:
3176 .   flg - PETSC_TRUE if the variable bounds function exists
3177 
3178     Level: developer
3179 
3180 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3181 
3182 @*/
3183 PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
3184 {
3185   PetscFunctionBegin;
3186   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
3187   PetscFunctionReturn(0);
3188 }
3189 
3190 /*@C
3191     DMComputeVariableBounds - compute variable bounds used by SNESVI.
3192 
3193     Logically Collective on DM
3194 
3195     Input Parameters:
3196 .   dm - the DM object
3197 
3198     Output parameters:
3199 +   xl - lower bound
3200 -   xu - upper bound
3201 
3202     Level: advanced
3203 
3204     Notes:
3205     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
3206 
3207 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3208 
3209 @*/
3210 PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
3211 {
3212   PetscErrorCode ierr;
3213 
3214   PetscFunctionBegin;
3215   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
3216   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
3217   if (dm->ops->computevariablebounds) {
3218     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
3219   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
3220   PetscFunctionReturn(0);
3221 }
3222 
3223 /*@
3224     DMHasColoring - does the DM object have a method of providing a coloring?
3225 
3226     Not Collective
3227 
3228     Input Parameter:
3229 .   dm - the DM object
3230 
3231     Output Parameter:
3232 .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3233 
3234     Level: developer
3235 
3236 .seealso DMHasFunction(), DMCreateColoring()
3237 
3238 @*/
3239 PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3240 {
3241   PetscFunctionBegin;
3242   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3243   PetscFunctionReturn(0);
3244 }
3245 
3246 /*@
3247     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
3248 
3249     Not Collective
3250 
3251     Input Parameter:
3252 .   dm - the DM object
3253 
3254     Output Parameter:
3255 .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
3256 
3257     Level: developer
3258 
3259 .seealso DMHasFunction(), DMCreateRestriction()
3260 
3261 @*/
3262 PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
3263 {
3264   PetscFunctionBegin;
3265   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
3266   PetscFunctionReturn(0);
3267 }
3268 
3269 
3270 /*@
3271     DMHasCreateInjection - does the DM object have a method of providing an injection?
3272 
3273     Not Collective
3274 
3275     Input Parameter:
3276 .   dm - the DM object
3277 
3278     Output Parameter:
3279 .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3280 
3281     Level: developer
3282 
3283 .seealso DMHasFunction(), DMCreateInjection()
3284 
3285 @*/
3286 PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3287 {
3288   PetscErrorCode ierr;
3289   PetscFunctionBegin;
3290   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
3291   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3292   PetscFunctionReturn(0);
3293 }
3294 
3295 /*@C
3296     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
3297 
3298     Collective on DM
3299 
3300     Input Parameter:
3301 +   dm - the DM object
3302 -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
3303 
3304     Level: developer
3305 
3306 .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
3307 
3308 @*/
3309 PetscErrorCode  DMSetVec(DM dm,Vec x)
3310 {
3311   PetscErrorCode ierr;
3312 
3313   PetscFunctionBegin;
3314   if (x) {
3315     if (!dm->x) {
3316       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
3317     }
3318     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
3319   } else if (dm->x) {
3320     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
3321   }
3322   PetscFunctionReturn(0);
3323 }
3324 
3325 PetscFunctionList DMList              = NULL;
3326 PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3327 
3328 /*@C
3329   DMSetType - Builds a DM, for a particular DM implementation.
3330 
3331   Collective on DM
3332 
3333   Input Parameters:
3334 + dm     - The DM object
3335 - method - The name of the DM type
3336 
3337   Options Database Key:
3338 . -dm_type <type> - Sets the DM type; use -help for a list of available types
3339 
3340   Notes:
3341   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3342 
3343   Level: intermediate
3344 
3345 .keywords: DM, set, type
3346 .seealso: DMGetType(), DMCreate()
3347 @*/
3348 PetscErrorCode  DMSetType(DM dm, DMType method)
3349 {
3350   PetscErrorCode (*r)(DM);
3351   PetscBool      match;
3352   PetscErrorCode ierr;
3353 
3354   PetscFunctionBegin;
3355   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3356   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3357   if (match) PetscFunctionReturn(0);
3358 
3359   ierr = DMRegisterAll();CHKERRQ(ierr);
3360   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3361   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3362 
3363   if (dm->ops->destroy) {
3364     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3365     dm->ops->destroy = NULL;
3366   }
3367   ierr = (*r)(dm);CHKERRQ(ierr);
3368   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3369   PetscFunctionReturn(0);
3370 }
3371 
3372 /*@C
3373   DMGetType - Gets the DM type name (as a string) from the DM.
3374 
3375   Not Collective
3376 
3377   Input Parameter:
3378 . dm  - The DM
3379 
3380   Output Parameter:
3381 . type - The DM type name
3382 
3383   Level: intermediate
3384 
3385 .keywords: DM, get, type, name
3386 .seealso: DMSetType(), DMCreate()
3387 @*/
3388 PetscErrorCode  DMGetType(DM dm, DMType *type)
3389 {
3390   PetscErrorCode ierr;
3391 
3392   PetscFunctionBegin;
3393   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3394   PetscValidPointer(type,2);
3395   ierr = DMRegisterAll();CHKERRQ(ierr);
3396   *type = ((PetscObject)dm)->type_name;
3397   PetscFunctionReturn(0);
3398 }
3399 
3400 /*@C
3401   DMConvert - Converts a DM to another DM, either of the same or different type.
3402 
3403   Collective on DM
3404 
3405   Input Parameters:
3406 + dm - the DM
3407 - newtype - new DM type (use "same" for the same type)
3408 
3409   Output Parameter:
3410 . M - pointer to new DM
3411 
3412   Notes:
3413   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
3414   the MPI communicator of the generated DM is always the same as the communicator
3415   of the input DM.
3416 
3417   Level: intermediate
3418 
3419 .seealso: DMCreate()
3420 @*/
3421 PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
3422 {
3423   DM             B;
3424   char           convname[256];
3425   PetscBool      sametype/*, issame */;
3426   PetscErrorCode ierr;
3427 
3428   PetscFunctionBegin;
3429   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3430   PetscValidType(dm,1);
3431   PetscValidPointer(M,3);
3432   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3433   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3434   if (sametype) {
3435     *M   = dm;
3436     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3437     PetscFunctionReturn(0);
3438   } else {
3439     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
3440 
3441     /*
3442        Order of precedence:
3443        1) See if a specialized converter is known to the current DM.
3444        2) See if a specialized converter is known to the desired DM class.
3445        3) See if a good general converter is registered for the desired class
3446        4) See if a good general converter is known for the current matrix.
3447        5) Use a really basic converter.
3448     */
3449 
3450     /* 1) See if a specialized converter is known to the current DM and the desired class */
3451     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3452     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3453     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3454     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3455     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
3456     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
3457     if (conv) goto foundconv;
3458 
3459     /* 2)  See if a specialized converter is known to the desired DM class. */
3460     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
3461     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3462     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3463     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3464     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3465     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3466     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
3467     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
3468     if (conv) {
3469       ierr = DMDestroy(&B);CHKERRQ(ierr);
3470       goto foundconv;
3471     }
3472 
3473 #if 0
3474     /* 3) See if a good general converter is registered for the desired class */
3475     conv = B->ops->convertfrom;
3476     ierr = DMDestroy(&B);CHKERRQ(ierr);
3477     if (conv) goto foundconv;
3478 
3479     /* 4) See if a good general converter is known for the current matrix */
3480     if (dm->ops->convert) {
3481       conv = dm->ops->convert;
3482     }
3483     if (conv) goto foundconv;
3484 #endif
3485 
3486     /* 5) Use a really basic converter. */
3487     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
3488 
3489 foundconv:
3490     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
3491     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
3492     /* Things that are independent of DM type: We should consult DMClone() here */
3493     {
3494       PetscBool             isper;
3495       const PetscReal      *maxCell, *L;
3496       const DMBoundaryType *bd;
3497       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
3498       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
3499     }
3500     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
3501   }
3502   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
3503   PetscFunctionReturn(0);
3504 }
3505 
3506 /*--------------------------------------------------------------------------------------------------------------------*/
3507 
3508 /*@C
3509   DMRegister -  Adds a new DM component implementation
3510 
3511   Not Collective
3512 
3513   Input Parameters:
3514 + name        - The name of a new user-defined creation routine
3515 - create_func - The creation routine itself
3516 
3517   Notes:
3518   DMRegister() may be called multiple times to add several user-defined DMs
3519 
3520 
3521   Sample usage:
3522 .vb
3523     DMRegister("my_da", MyDMCreate);
3524 .ve
3525 
3526   Then, your DM type can be chosen with the procedural interface via
3527 .vb
3528     DMCreate(MPI_Comm, DM *);
3529     DMSetType(DM,"my_da");
3530 .ve
3531    or at runtime via the option
3532 .vb
3533     -da_type my_da
3534 .ve
3535 
3536   Level: advanced
3537 
3538 .keywords: DM, register
3539 .seealso: DMRegisterAll(), DMRegisterDestroy()
3540 
3541 @*/
3542 PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3543 {
3544   PetscErrorCode ierr;
3545 
3546   PetscFunctionBegin;
3547   ierr = DMInitializePackage();CHKERRQ(ierr);
3548   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3549   PetscFunctionReturn(0);
3550 }
3551 
3552 /*@C
3553   DMLoad - Loads a DM that has been stored in binary  with DMView().
3554 
3555   Collective on PetscViewer
3556 
3557   Input Parameters:
3558 + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3559            some related function before a call to DMLoad().
3560 - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3561            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3562 
3563    Level: intermediate
3564 
3565   Notes:
3566    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3567 
3568   Notes for advanced users:
3569   Most users should not need to know the details of the binary storage
3570   format, since DMLoad() and DMView() completely hide these details.
3571   But for anyone who's interested, the standard binary matrix storage
3572   format is
3573 .vb
3574      has not yet been determined
3575 .ve
3576 
3577 .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3578 @*/
3579 PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3580 {
3581   PetscBool      isbinary, ishdf5;
3582   PetscErrorCode ierr;
3583 
3584   PetscFunctionBegin;
3585   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3586   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3587   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
3588   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
3589   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
3590   if (isbinary) {
3591     PetscInt classid;
3592     char     type[256];
3593 
3594     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
3595     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3596     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
3597     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
3598     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
3599   } else if (ishdf5) {
3600     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
3601   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3602   PetscFunctionReturn(0);
3603 }
3604 
3605 /******************************** FEM Support **********************************/
3606 
3607 PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3608 {
3609   PetscInt       f;
3610   PetscErrorCode ierr;
3611 
3612   PetscFunctionBegin;
3613   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
3614   for (f = 0; f < len; ++f) {
3615     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
3616   }
3617   PetscFunctionReturn(0);
3618 }
3619 
3620 PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3621 {
3622   PetscInt       f, g;
3623   PetscErrorCode ierr;
3624 
3625   PetscFunctionBegin;
3626   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
3627   for (f = 0; f < rows; ++f) {
3628     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
3629     for (g = 0; g < cols; ++g) {
3630       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
3631     }
3632     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
3633   }
3634   PetscFunctionReturn(0);
3635 }
3636 
3637 PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3638 {
3639   PetscInt          localSize, bs;
3640   PetscMPIInt       size;
3641   Vec               x, xglob;
3642   const PetscScalar *xarray;
3643   PetscErrorCode    ierr;
3644 
3645   PetscFunctionBegin;
3646   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3647   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3648   ierr = VecCopy(X, x);CHKERRQ(ierr);
3649   ierr = VecChop(x, tol);CHKERRQ(ierr);
3650   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
3651   if (size > 1) {
3652     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
3653     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
3654     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
3655     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
3656   } else {
3657     xglob = x;
3658   }
3659   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
3660   if (size > 1) {
3661     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
3662     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
3663   }
3664   ierr = VecDestroy(&x);CHKERRQ(ierr);
3665   PetscFunctionReturn(0);
3666 }
3667 
3668 /*@
3669   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
3670 
3671   Input Parameter:
3672 . dm - The DM
3673 
3674   Output Parameter:
3675 . section - The PetscSection
3676 
3677   Options Database Keys:
3678 . -dm_petscsection_view - View the Section created by the DM
3679 
3680   Level: intermediate
3681 
3682   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
3683 
3684 .seealso: DMSetSection(), DMGetGlobalSection()
3685 @*/
3686 PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3687 {
3688   PetscErrorCode ierr;
3689 
3690   PetscFunctionBegin;
3691   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3692   PetscValidPointer(section, 2);
3693   if (!dm->defaultSection && dm->ops->createdefaultsection) {
3694     PetscInt d;
3695 
3696     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
3697     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3698     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
3699   }
3700   *section = dm->defaultSection;
3701   PetscFunctionReturn(0);
3702 }
3703 
3704 /*@
3705   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
3706 
3707   Input Parameters:
3708 + dm - The DM
3709 - section - The PetscSection
3710 
3711   Level: intermediate
3712 
3713   Note: Any existing Section will be destroyed
3714 
3715 .seealso: DMSetSection(), DMGetGlobalSection()
3716 @*/
3717 PetscErrorCode DMSetSection(DM dm, PetscSection section)
3718 {
3719   PetscInt       numFields = 0;
3720   PetscInt       f;
3721   PetscErrorCode ierr;
3722 
3723   PetscFunctionBegin;
3724   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3725   if (section) {
3726     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3727     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3728   }
3729   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
3730   dm->defaultSection = section;
3731   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3732   if (numFields) {
3733     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3734     for (f = 0; f < numFields; ++f) {
3735       PetscObject disc;
3736       const char *name;
3737 
3738       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
3739       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
3740       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3741     }
3742   }
3743   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
3744   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3745   PetscFunctionReturn(0);
3746 }
3747 
3748 /*@
3749   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
3750 
3751   not collective
3752 
3753   Input Parameter:
3754 . dm - The DM
3755 
3756   Output Parameter:
3757 + section - The PetscSection describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section.  Returns NULL if there are no local constraints.
3758 - mat - The Mat that interpolates local constraints: its width should be the layout size of the default section.  Returns NULL if there are no local constraints.
3759 
3760   Level: advanced
3761 
3762   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
3763 
3764 .seealso: DMSetDefaultConstraints()
3765 @*/
3766 PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
3767 {
3768   PetscErrorCode ierr;
3769 
3770   PetscFunctionBegin;
3771   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3772   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
3773   if (section) {*section = dm->defaultConstraintSection;}
3774   if (mat) {*mat = dm->defaultConstraintMat;}
3775   PetscFunctionReturn(0);
3776 }
3777 
3778 /*@
3779   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
3780 
3781   If a constraint matrix is specified, then it is applied during DMGlobalToLocalEnd() when mode is INSERT_VALUES, INSERT_BC_VALUES, or INSERT_ALL_VALUES.  Without a constraint matrix, the local vector l returned by DMGlobalToLocalEnd() contains values that have been scattered from a global vector without modification; with a constraint matrix A, l is modified by computing c = A * l, l[s[i]] = c[i], where the scatter s is defined by the PetscSection returned by DMGetDefaultConstraintMatrix().
3782 
3783   If a constraint matrix is specified, then its adjoint is applied during DMLocalToGlobalBegin() when mode is ADD_VALUES, ADD_BC_VALUES, or ADD_ALL_VALUES.  Without a constraint matrix, the local vector l is accumulated into a global vector without modification; with a constraint matrix A, l is first modified by computing c[i] = l[s[i]], l[s[i]] = 0, l = l + A'*c, which is the adjoint of the operation described above.
3784 
3785   collective on dm
3786 
3787   Input Parameters:
3788 + dm - The DM
3789 + section - The PetscSection describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section.  Must have a local communicator (PETSC_COMM_SELF or derivative).
3790 - mat - The Mat that interpolates local constraints: its width should be the layout size of the default section:  NULL indicates no constraints.  Must have a local communicator (PETSC_COMM_SELF or derivative).
3791 
3792   Level: advanced
3793 
3794   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
3795 
3796 .seealso: DMGetDefaultConstraints()
3797 @*/
3798 PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
3799 {
3800   PetscMPIInt result;
3801   PetscErrorCode ierr;
3802 
3803   PetscFunctionBegin;
3804   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3805   if (section) {
3806     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3807     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3808     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3809   }
3810   if (mat) {
3811     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3812     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3813     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3814   }
3815   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3816   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
3817   dm->defaultConstraintSection = section;
3818   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
3819   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
3820   dm->defaultConstraintMat = mat;
3821   PetscFunctionReturn(0);
3822 }
3823 
3824 #if defined(PETSC_USE_DEBUG)
3825 /*
3826   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3827 
3828   Input Parameters:
3829 + dm - The DM
3830 . localSection - PetscSection describing the local data layout
3831 - globalSection - PetscSection describing the global data layout
3832 
3833   Level: intermediate
3834 
3835 .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3836 */
3837 static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3838 {
3839   MPI_Comm        comm;
3840   PetscLayout     layout;
3841   const PetscInt *ranges;
3842   PetscInt        pStart, pEnd, p, nroots;
3843   PetscMPIInt     size, rank;
3844   PetscBool       valid = PETSC_TRUE, gvalid;
3845   PetscErrorCode  ierr;
3846 
3847   PetscFunctionBegin;
3848   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3849   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3850   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3851   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3852   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3853   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3854   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3855   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3856   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3857   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3858   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3859   for (p = pStart; p < pEnd; ++p) {
3860     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3861 
3862     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3863     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3864     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3865     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3866     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3867     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3868     if (!gdof) continue; /* Censored point */
3869     if ((gdof < 0 ? -(gdof+1) : gdof) != dof) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global dof %d for point %d not equal to local dof %d\n", rank, gdof, p, dof);CHKERRQ(ierr); valid = PETSC_FALSE;}
3870     if (gcdof && (gcdof != cdof)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global constraints %d for point %d not equal to local constraints %d\n", rank, gcdof, p, cdof);CHKERRQ(ierr); valid = PETSC_FALSE;}
3871     if (gdof < 0) {
3872       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3873       for (d = 0; d < gsize; ++d) {
3874         PetscInt offset = -(goff+1) + d, r;
3875 
3876         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3877         if (r < 0) r = -(r+2);
3878         if ((r < 0) || (r >= size)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Point %d mapped to invalid process %d (%d, %d)\n", rank, p, r, gdof, goff);CHKERRQ(ierr); valid = PETSC_FALSE;break;}
3879       }
3880     }
3881   }
3882   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3883   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3884   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3885   if (!gvalid) {
3886     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3887     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3888   }
3889   PetscFunctionReturn(0);
3890 }
3891 #endif
3892 
3893 /*@
3894   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
3895 
3896   Collective on DM
3897 
3898   Input Parameter:
3899 . dm - The DM
3900 
3901   Output Parameter:
3902 . section - The PetscSection
3903 
3904   Level: intermediate
3905 
3906   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
3907 
3908 .seealso: DMSetSection(), DMGetSection()
3909 @*/
3910 PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
3911 {
3912   PetscErrorCode ierr;
3913 
3914   PetscFunctionBegin;
3915   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3916   PetscValidPointer(section, 2);
3917   if (!dm->defaultGlobalSection) {
3918     PetscSection s;
3919 
3920     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3921     if (!s)  SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
3922     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
3923     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3924     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3925     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3926     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
3927   }
3928   *section = dm->defaultGlobalSection;
3929   PetscFunctionReturn(0);
3930 }
3931 
3932 /*@
3933   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3934 
3935   Input Parameters:
3936 + dm - The DM
3937 - section - The PetscSection, or NULL
3938 
3939   Level: intermediate
3940 
3941   Note: Any existing Section will be destroyed
3942 
3943 .seealso: DMGetGlobalSection(), DMSetSection()
3944 @*/
3945 PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
3946 {
3947   PetscErrorCode ierr;
3948 
3949   PetscFunctionBegin;
3950   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3951   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3952   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3953   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3954   dm->defaultGlobalSection = section;
3955 #if defined(PETSC_USE_DEBUG)
3956   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3957 #endif
3958   PetscFunctionReturn(0);
3959 }
3960 
3961 /*@
3962   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
3963   it is created from the default PetscSection layouts in the DM.
3964 
3965   Input Parameter:
3966 . dm - The DM
3967 
3968   Output Parameter:
3969 . sf - The PetscSF
3970 
3971   Level: intermediate
3972 
3973   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3974 
3975 .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
3976 @*/
3977 PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
3978 {
3979   PetscInt       nroots;
3980   PetscErrorCode ierr;
3981 
3982   PetscFunctionBegin;
3983   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3984   PetscValidPointer(sf, 2);
3985   if (!dm->defaultSF) {
3986     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
3987   }
3988   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
3989   if (nroots < 0) {
3990     PetscSection section, gSection;
3991 
3992     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
3993     if (section) {
3994       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
3995       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
3996     } else {
3997       *sf = NULL;
3998       PetscFunctionReturn(0);
3999     }
4000   }
4001   *sf = dm->defaultSF;
4002   PetscFunctionReturn(0);
4003 }
4004 
4005 /*@
4006   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
4007 
4008   Input Parameters:
4009 + dm - The DM
4010 - sf - The PetscSF
4011 
4012   Level: intermediate
4013 
4014   Note: Any previous SF is destroyed
4015 
4016 .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
4017 @*/
4018 PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
4019 {
4020   PetscErrorCode ierr;
4021 
4022   PetscFunctionBegin;
4023   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4024   if (sf) {
4025     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4026     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4027   }
4028   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
4029   dm->defaultSF = sf;
4030   PetscFunctionReturn(0);
4031 }
4032 
4033 /*@C
4034   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
4035   describing the data layout.
4036 
4037   Input Parameters:
4038 + dm - The DM
4039 . localSection - PetscSection describing the local data layout
4040 - globalSection - PetscSection describing the global data layout
4041 
4042   Level: intermediate
4043 
4044 .seealso: DMGetDefaultSF(), DMSetDefaultSF()
4045 @*/
4046 PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
4047 {
4048   MPI_Comm       comm;
4049   PetscLayout    layout;
4050   const PetscInt *ranges;
4051   PetscInt       *local;
4052   PetscSFNode    *remote;
4053   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
4054   PetscMPIInt    size, rank;
4055   PetscErrorCode ierr;
4056 
4057   PetscFunctionBegin;
4058   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4059   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4060   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4061   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4062   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4063   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4064   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4065   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4066   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4067   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4068   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4069   for (p = pStart; p < pEnd; ++p) {
4070     PetscInt gdof, gcdof;
4071 
4072     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4073     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4074     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
4075     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4076   }
4077   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4078   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
4079   for (p = pStart, l = 0; p < pEnd; ++p) {
4080     const PetscInt *cind;
4081     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
4082 
4083     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4084     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4085     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4086     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
4087     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4088     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4089     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4090     if (!gdof) continue; /* Censored point */
4091     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4092     if (gsize != dof-cdof) {
4093       if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof);
4094       cdof = 0; /* Ignore constraints */
4095     }
4096     for (d = 0, c = 0; d < dof; ++d) {
4097       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
4098       local[l+d-c] = off+d;
4099     }
4100     if (gdof < 0) {
4101       for (d = 0; d < gsize; ++d, ++l) {
4102         PetscInt offset = -(goff+1) + d, r;
4103 
4104         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4105         if (r < 0) r = -(r+2);
4106         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
4107         remote[l].rank  = r;
4108         remote[l].index = offset - ranges[r];
4109       }
4110     } else {
4111       for (d = 0; d < gsize; ++d, ++l) {
4112         remote[l].rank  = rank;
4113         remote[l].index = goff+d - ranges[rank];
4114       }
4115     }
4116   }
4117   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
4118   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4119   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
4120   PetscFunctionReturn(0);
4121 }
4122 
4123 /*@
4124   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4125 
4126   Input Parameter:
4127 . dm - The DM
4128 
4129   Output Parameter:
4130 . sf - The PetscSF
4131 
4132   Level: intermediate
4133 
4134   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4135 
4136 .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4137 @*/
4138 PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
4139 {
4140   PetscFunctionBegin;
4141   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4142   PetscValidPointer(sf, 2);
4143   *sf = dm->sf;
4144   PetscFunctionReturn(0);
4145 }
4146 
4147 /*@
4148   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4149 
4150   Input Parameters:
4151 + dm - The DM
4152 - sf - The PetscSF
4153 
4154   Level: intermediate
4155 
4156 .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4157 @*/
4158 PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
4159 {
4160   PetscErrorCode ierr;
4161 
4162   PetscFunctionBegin;
4163   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4164   if (sf) {
4165     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4166     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4167   }
4168   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4169   dm->sf = sf;
4170   PetscFunctionReturn(0);
4171 }
4172 
4173 static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
4174 {
4175   RegionField   *tmpr;
4176   PetscInt       Nf = dm->Nf, f;
4177   PetscErrorCode ierr;
4178 
4179   PetscFunctionBegin;
4180   if (Nf >= NfNew) PetscFunctionReturn(0);
4181   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
4182   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
4183   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
4184   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
4185   dm->Nf     = NfNew;
4186   dm->fields = tmpr;
4187   PetscFunctionReturn(0);
4188 }
4189 
4190 /*@
4191   DMClearFields - Remove all fields from the DM
4192 
4193   Logically collective on DM
4194 
4195   Input Parameter:
4196 . dm - The DM
4197 
4198   Level: intermediate
4199 
4200 .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
4201 @*/
4202 PetscErrorCode DMClearFields(DM dm)
4203 {
4204   PetscInt       f;
4205   PetscErrorCode ierr;
4206 
4207   PetscFunctionBegin;
4208   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4209   for (f = 0; f < dm->Nf; ++f) {
4210     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4211     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4212   }
4213   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
4214   dm->fields = NULL;
4215   dm->Nf     = 0;
4216   PetscFunctionReturn(0);
4217 }
4218 
4219 /*@
4220   DMGetNumFields - Get the number of fields in the DM
4221 
4222   Not collective
4223 
4224   Input Parameter:
4225 . dm - The DM
4226 
4227   Output Parameter:
4228 . Nf - The number of fields
4229 
4230   Level: intermediate
4231 
4232 .seealso: DMSetNumFields(), DMSetField()
4233 @*/
4234 PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
4235 {
4236   PetscFunctionBegin;
4237   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4238   PetscValidPointer(numFields, 2);
4239   *numFields = dm->Nf;
4240   PetscFunctionReturn(0);
4241 }
4242 
4243 /*@
4244   DMSetNumFields - Set the number of fields in the DM
4245 
4246   Logically collective on DM
4247 
4248   Input Parameters:
4249 + dm - The DM
4250 - Nf - The number of fields
4251 
4252   Level: intermediate
4253 
4254 .seealso: DMGetNumFields(), DMSetField()
4255 @*/
4256 PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4257 {
4258   PetscInt       Nf, f;
4259   PetscErrorCode ierr;
4260 
4261   PetscFunctionBegin;
4262   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4263   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4264   for (f = Nf; f < numFields; ++f) {
4265     PetscContainer obj;
4266 
4267     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
4268     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
4269     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4270   }
4271   PetscFunctionReturn(0);
4272 }
4273 
4274 /*@
4275   DMGetField - Return the discretization object for a given DM field
4276 
4277   Not collective
4278 
4279   Input Parameters:
4280 + dm - The DM
4281 - f  - The field number
4282 
4283   Output Parameters:
4284 + label - The label indicating the support of the field, or NULL for the entire mesh
4285 - field - The discretization object
4286 
4287   Level: intermediate
4288 
4289 .seealso: DMAddField(), DMSetField()
4290 @*/
4291 PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4292 {
4293   PetscFunctionBegin;
4294   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4295   PetscValidPointer(field, 3);
4296   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
4297   if (label) *label = dm->fields[f].label;
4298   if (field) *field = dm->fields[f].disc;
4299   PetscFunctionReturn(0);
4300 }
4301 
4302 /*@
4303   DMSetField - Set the discretization object for a given DM field
4304 
4305   Logically collective on DM
4306 
4307   Input Parameters:
4308 + dm    - The DM
4309 . f     - The field number
4310 . label - The label indicating the support of the field, or NULL for the entire mesh
4311 - field - The discretization object
4312 
4313   Level: intermediate
4314 
4315 .seealso: DMAddField(), DMGetField()
4316 @*/
4317 PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4318 {
4319   PetscErrorCode ierr;
4320 
4321   PetscFunctionBegin;
4322   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4323   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4324   PetscValidHeader(field, 4);
4325   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4326   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4327   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4328   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4329   dm->fields[f].label = label;
4330   dm->fields[f].disc  = field;
4331   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4332   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4333   PetscFunctionReturn(0);
4334 }
4335 
4336 /*@
4337   DMAddField - Add the discretization object for the given DM field
4338 
4339   Logically collective on DM
4340 
4341   Input Parameters:
4342 + dm    - The DM
4343 . label - The label indicating the support of the field, or NULL for the entire mesh
4344 - field - The discretization object
4345 
4346   Level: intermediate
4347 
4348 .seealso: DMSetField(), DMGetField()
4349 @*/
4350 PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
4351 {
4352   PetscInt       Nf = dm->Nf;
4353   PetscErrorCode ierr;
4354 
4355   PetscFunctionBegin;
4356   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4357   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
4358   PetscValidHeader(field, 3);
4359   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
4360   dm->fields[Nf].label = label;
4361   dm->fields[Nf].disc  = field;
4362   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4363   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4364   PetscFunctionReturn(0);
4365 }
4366 
4367 /*@
4368   DMCopyFields - Copy the discretizations for the DM into another DM
4369 
4370   Collective on DM
4371 
4372   Input Parameter:
4373 . dm - The DM
4374 
4375   Output Parameter:
4376 . newdm - The DM
4377 
4378   Level: advanced
4379 
4380 .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4381 @*/
4382 PetscErrorCode DMCopyFields(DM dm, DM newdm)
4383 {
4384   PetscInt       Nf, f;
4385   PetscErrorCode ierr;
4386 
4387   PetscFunctionBegin;
4388   if (dm == newdm) PetscFunctionReturn(0);
4389   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4390   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4391   for (f = 0; f < Nf; ++f) {
4392     DMLabel     label;
4393     PetscObject field;
4394 
4395     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4396     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
4397   }
4398   PetscFunctionReturn(0);
4399 }
4400 
4401 static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4402 {
4403   DMSpace       *tmpd;
4404   PetscInt       Nds = dm->Nds, s;
4405   PetscErrorCode ierr;
4406 
4407   PetscFunctionBegin;
4408   if (Nds >= NdsNew) PetscFunctionReturn(0);
4409   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4410   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4411   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL;}
4412   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4413   dm->Nds   = NdsNew;
4414   dm->probs = tmpd;
4415   PetscFunctionReturn(0);
4416 }
4417 
4418 /*@
4419   DMGetNumDS - Get the number of discrete systems in the DM
4420 
4421   Not collective
4422 
4423   Input Parameter:
4424 . dm - The DM
4425 
4426   Output Parameter:
4427 . Nds - The number of PetscDS objects
4428 
4429   Level: intermediate
4430 
4431 .seealso: DMGetDS(), DMGetCellDS()
4432 @*/
4433 PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4434 {
4435   PetscFunctionBegin;
4436   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4437   PetscValidPointer(Nds, 2);
4438   *Nds = dm->Nds;
4439   PetscFunctionReturn(0);
4440 }
4441 
4442 /*@
4443   DMClearDS - Remove all discrete systems from the DM
4444 
4445   Logically collective on DM
4446 
4447   Input Parameter:
4448 . dm - The DM
4449 
4450   Level: intermediate
4451 
4452 .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4453 @*/
4454 PetscErrorCode DMClearDS(DM dm)
4455 {
4456   PetscInt       s;
4457   PetscErrorCode ierr;
4458 
4459   PetscFunctionBegin;
4460   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4461   for (s = 0; s < dm->Nds; ++s) {
4462     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4463     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4464   }
4465   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4466   dm->probs = NULL;
4467   dm->Nds   = 0;
4468   PetscFunctionReturn(0);
4469 }
4470 
4471 /*@
4472   DMGetDS - Get the default PetscDS
4473 
4474   Not collective
4475 
4476   Input Parameter:
4477 . dm    - The DM
4478 
4479   Output Parameter:
4480 . prob - The default PetscDS
4481 
4482   Level: intermediate
4483 
4484 .seealso: DMGetCellDS(), DMGetRegionDS()
4485 @*/
4486 PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4487 {
4488   PetscFunctionBeginHot;
4489   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4490   PetscValidPointer(prob, 2);
4491   if (dm->Nds) *prob = dm->probs[0].ds;
4492   else         *prob = NULL;
4493   PetscFunctionReturn(0);
4494 }
4495 
4496 /*@
4497   DMGetCellDS - Get the PetscDS defined on a given cell
4498 
4499   Not collective
4500 
4501   Input Parameters:
4502 + dm    - The DM
4503 - point - Cell for the DS
4504 
4505   Output Parameter:
4506 . prob - The PetscDS defined on the given cell
4507 
4508   Level: developer
4509 
4510 .seealso: DMGetDS(), DMSetDS()
4511 @*/
4512 PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4513 {
4514   PetscDS        probDef = NULL;
4515   PetscInt       s;
4516   PetscErrorCode ierr;
4517 
4518   PetscFunctionBeginHot;
4519   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4520   PetscValidPointer(prob, 3);
4521   *prob = NULL;
4522   for (s = 0; s < dm->Nds; ++s) {
4523     PetscInt val;
4524 
4525     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4526     else {
4527       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4528       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4529     }
4530   }
4531   if (!*prob) *prob = probDef;
4532   PetscFunctionReturn(0);
4533 }
4534 
4535 /*@
4536   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4537 
4538   Not collective
4539 
4540   Input Parameters:
4541 + dm    - The DM
4542 - label - The DMLabel defining the mesh region, or NULL for the entire mesh
4543 
4544   Output Parameter:
4545 . prob - The PetscDS defined on the given region
4546 
4547   Note: If the label is missing, this function returns an error
4548 
4549   Level: advanced
4550 
4551 .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4552 @*/
4553 PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, PetscDS *ds)
4554 {
4555   PetscInt Nds = dm->Nds, s;
4556 
4557   PetscFunctionBegin;
4558   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4559   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4560   PetscValidPointer(ds, 3);
4561   *ds = NULL;
4562   for (s = 0; s < Nds; ++s) {
4563     if (dm->probs[s].label == label) {*ds = dm->probs[s].ds; PetscFunctionReturn(0);}
4564   }
4565   SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Label not found in DM");
4566 }
4567 
4568 /*@
4569   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
4570 
4571   Not collective
4572 
4573   Input Parameters:
4574 + dm  - The DM
4575 - num - The region number, in [0, Nds)
4576 
4577   Output Parameters:
4578 + label - The region label
4579 - prob  - The PetscDS defined on the given region
4580 
4581   Level: advanced
4582 
4583 .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
4584 @*/
4585 PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, PetscDS *ds)
4586 {
4587   PetscInt       Nds;
4588   PetscErrorCode ierr;
4589 
4590   PetscFunctionBegin;
4591   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4592   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4593   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
4594   if (label) {
4595     PetscValidPointer(label, 3);
4596     *label = dm->probs[num].label;
4597   }
4598   if (ds) {
4599     PetscValidPointer(ds, 4);
4600     *ds = dm->probs[num].ds;
4601   }
4602   PetscFunctionReturn(0);
4603 }
4604 
4605 /*@
4606   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
4607 
4608   Collective on DM
4609 
4610   Input Parameters:
4611 + dm    - The DM
4612 . label - The DMLabel defining the mesh region, or NULL for the entire mesh
4613 - prob - The PetscDS defined on the given cell
4614 
4615   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4616 
4617   Level: advanced
4618 
4619 .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
4620 @*/
4621 PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, PetscDS ds)
4622 {
4623   PetscInt       Nds = dm->Nds, s;
4624   PetscErrorCode ierr;
4625 
4626   PetscFunctionBegin;
4627   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4628   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
4629   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
4630   for (s = 0; s < Nds; ++s) {
4631     if (dm->probs[s].label == label) {
4632       dm->probs[s].ds = ds;
4633       PetscFunctionReturn(0);
4634     }
4635   }
4636   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
4637   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4638   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
4639   if (!label) {
4640     /* Put the NULL label at the front, so it is returned as the default */
4641     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
4642     Nds = 0;
4643   }
4644   dm->probs[Nds].label = label;
4645   dm->probs[Nds].ds    = ds;
4646   PetscFunctionReturn(0);
4647 }
4648 
4649 /*@
4650   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
4651 
4652   Collective on DM
4653 
4654   Input Parameter:
4655 . dm - The DM
4656 
4657   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
4658 
4659   Level: intermediate
4660 
4661 .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4662 @*/
4663 PetscErrorCode DMCreateDS(DM dm)
4664 {
4665   MPI_Comm       comm;
4666   PetscDS        prob, probh = NULL;
4667   PetscInt       dimEmbed, f, s, field = 0, fieldh = 0;
4668   PetscBool      doSetup = PETSC_TRUE;
4669   PetscErrorCode ierr;
4670 
4671   PetscFunctionBegin;
4672   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4673   if (!dm->fields) PetscFunctionReturn(0);
4674   /* Can only handle two label cases right now:
4675    1) NULL
4676    2) Hybrid cells
4677   */
4678   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
4679   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4680   /* Create default DS */
4681   ierr = DMGetRegionDS(dm, NULL, &prob);CHKERRQ(ierr);
4682   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
4683   /* Optionally create hybrid DS */
4684   for (f = 0; f < dm->Nf; ++f) {
4685     DMLabel  label = dm->fields[f].label;
4686     PetscInt lStart, lEnd;
4687 
4688     if (label) {
4689       DM       plex;
4690       PetscInt depth, pMax[4];
4691 
4692       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
4693       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
4694       ierr = DMPlexGetHybridBounds(plex, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
4695       ierr = DMDestroy(&plex);CHKERRQ(ierr);
4696 
4697       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
4698       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
4699       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
4700       ierr = DMSetRegionDS(dm, label, probh);CHKERRQ(ierr);
4701       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
4702       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
4703       break;
4704     }
4705   }
4706   /* Set fields in DSes */
4707   for (f = 0; f < dm->Nf; ++f) {
4708     DMLabel     label = dm->fields[f].label;
4709     PetscObject disc  = dm->fields[f].disc;
4710 
4711     if (!label) {
4712       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
4713       if (probh) {
4714         PetscFE subfe;
4715 
4716         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
4717         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
4718       }
4719     } else {
4720       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
4721     }
4722     /* We allow people to have placeholder fields and construct the Section by hand */
4723     {
4724       PetscClassId id;
4725 
4726       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
4727       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
4728     }
4729   }
4730   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
4731   /* Setup DSes */
4732   if (doSetup) {
4733     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
4734   }
4735   PetscFunctionReturn(0);
4736 }
4737 
4738 /*@
4739   DMCopyDS - Copy the discrete systems for the DM into another DM
4740 
4741   Collective on DM
4742 
4743   Input Parameter:
4744 . dm - The DM
4745 
4746   Output Parameter:
4747 . newdm - The DM
4748 
4749   Level: advanced
4750 
4751 .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
4752 @*/
4753 PetscErrorCode DMCopyDS(DM dm, DM newdm)
4754 {
4755   PetscInt       Nds, s;
4756   PetscErrorCode ierr;
4757 
4758   PetscFunctionBegin;
4759   if (dm == newdm) PetscFunctionReturn(0);
4760   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
4761   ierr = DMClearDS(newdm);CHKERRQ(ierr);
4762   for (s = 0; s < Nds; ++s) {
4763     DMLabel label;
4764     PetscDS ds;
4765 
4766     ierr = DMGetRegionNumDS(dm, s, &label, &ds);CHKERRQ(ierr);
4767     ierr = DMSetRegionDS(newdm, label, ds);CHKERRQ(ierr);
4768   }
4769   PetscFunctionReturn(0);
4770 }
4771 
4772 /*@
4773   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
4774 
4775   Collective on DM
4776 
4777   Input Parameter:
4778 . dm - The DM
4779 
4780   Output Parameter:
4781 . newdm - The DM
4782 
4783   Level: advanced
4784 
4785 .seealso: DMCopyFields(), DMCopyDS()
4786 @*/
4787 PetscErrorCode DMCopyDisc(DM dm, DM newdm)
4788 {
4789   PetscErrorCode ierr;
4790 
4791   PetscFunctionBegin;
4792   if (dm == newdm) PetscFunctionReturn(0);
4793   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
4794   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
4795   PetscFunctionReturn(0);
4796 }
4797 
4798 PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4799 {
4800   DM dm_coord,dmc_coord;
4801   PetscErrorCode ierr;
4802   Vec coords,ccoords;
4803   Mat inject;
4804   PetscFunctionBegin;
4805   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4806   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4807   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4808   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4809   if (coords && !ccoords) {
4810     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
4811     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
4812     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
4813     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
4814     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4815     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4816     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4817   }
4818   PetscFunctionReturn(0);
4819 }
4820 
4821 static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
4822 {
4823   DM dm_coord,subdm_coord;
4824   PetscErrorCode ierr;
4825   Vec coords,ccoords,clcoords;
4826   VecScatter *scat_i,*scat_g;
4827   PetscFunctionBegin;
4828   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4829   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
4830   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4831   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
4832   if (coords && !ccoords) {
4833     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
4834     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
4835     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
4836     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
4837     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
4838     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4839     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4840     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4841     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
4842     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
4843     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
4844     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
4845     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
4846     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4847     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
4848     ierr = PetscFree(scat_i);CHKERRQ(ierr);
4849     ierr = PetscFree(scat_g);CHKERRQ(ierr);
4850   }
4851   PetscFunctionReturn(0);
4852 }
4853 
4854 /*@
4855   DMGetDimension - Return the topological dimension of the DM
4856 
4857   Not collective
4858 
4859   Input Parameter:
4860 . dm - The DM
4861 
4862   Output Parameter:
4863 . dim - The topological dimension
4864 
4865   Level: beginner
4866 
4867 .seealso: DMSetDimension(), DMCreate()
4868 @*/
4869 PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4870 {
4871   PetscFunctionBegin;
4872   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4873   PetscValidPointer(dim, 2);
4874   *dim = dm->dim;
4875   PetscFunctionReturn(0);
4876 }
4877 
4878 /*@
4879   DMSetDimension - Set the topological dimension of the DM
4880 
4881   Collective on dm
4882 
4883   Input Parameters:
4884 + dm - The DM
4885 - dim - The topological dimension
4886 
4887   Level: beginner
4888 
4889 .seealso: DMGetDimension(), DMCreate()
4890 @*/
4891 PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4892 {
4893   PetscDS        ds;
4894   PetscErrorCode ierr;
4895 
4896   PetscFunctionBegin;
4897   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4898   PetscValidLogicalCollectiveInt(dm, dim, 2);
4899   dm->dim = dim;
4900   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
4901   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
4902   PetscFunctionReturn(0);
4903 }
4904 
4905 /*@
4906   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4907 
4908   Collective on DM
4909 
4910   Input Parameters:
4911 + dm - the DM
4912 - dim - the dimension
4913 
4914   Output Parameters:
4915 + pStart - The first point of the given dimension
4916 . pEnd - The first point following points of the given dimension
4917 
4918   Note:
4919   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4920   http://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
4921   then the interval is empty.
4922 
4923   Level: intermediate
4924 
4925 .keywords: point, Hasse Diagram, dimension
4926 .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4927 @*/
4928 PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4929 {
4930   PetscInt       d;
4931   PetscErrorCode ierr;
4932 
4933   PetscFunctionBegin;
4934   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4935   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4936   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4937   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4938   PetscFunctionReturn(0);
4939 }
4940 
4941 /*@
4942   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
4943 
4944   Collective on DM
4945 
4946   Input Parameters:
4947 + dm - the DM
4948 - c - coordinate vector
4949 
4950   Notes:
4951   The coordinates do include those for ghost points, which are in the local vector.
4952 
4953   The vector c should be destroyed by the caller.
4954 
4955   Level: intermediate
4956 
4957 .keywords: distributed array, get, corners, nodes, local indices, coordinates
4958 .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
4959 @*/
4960 PetscErrorCode DMSetCoordinates(DM dm, Vec c)
4961 {
4962   PetscErrorCode ierr;
4963 
4964   PetscFunctionBegin;
4965   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4966   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
4967   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
4968   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
4969   dm->coordinates = c;
4970   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4971   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
4972   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
4973   PetscFunctionReturn(0);
4974 }
4975 
4976 /*@
4977   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
4978 
4979   Not collective
4980 
4981    Input Parameters:
4982 +  dm - the DM
4983 -  c - coordinate vector
4984 
4985   Notes:
4986   The coordinates of ghost points can be set using DMSetCoordinates()
4987   followed by DMGetCoordinatesLocal(). This is intended to enable the
4988   setting of ghost coordinates outside of the domain.
4989 
4990   The vector c should be destroyed by the caller.
4991 
4992   Level: intermediate
4993 
4994 .keywords: distributed array, get, corners, nodes, local indices, coordinates
4995 .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
4996 @*/
4997 PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
4998 {
4999   PetscErrorCode ierr;
5000 
5001   PetscFunctionBegin;
5002   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5003   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
5004   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
5005   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5006 
5007   dm->coordinatesLocal = c;
5008 
5009   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
5010   PetscFunctionReturn(0);
5011 }
5012 
5013 /*@
5014   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
5015 
5016   Collective on DM
5017 
5018   Input Parameter:
5019 . dm - the DM
5020 
5021   Output Parameter:
5022 . c - global coordinate vector
5023 
5024   Note:
5025   This is a borrowed reference, so the user should NOT destroy this vector
5026 
5027   Each process has only the local coordinates (does NOT have the ghost coordinates).
5028 
5029   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
5030   and (x_0,y_0,z_0,x_1,y_1,z_1...)
5031 
5032   Level: intermediate
5033 
5034 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5035 .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
5036 @*/
5037 PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
5038 {
5039   PetscErrorCode ierr;
5040 
5041   PetscFunctionBegin;
5042   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5043   PetscValidPointer(c,2);
5044   if (!dm->coordinates && dm->coordinatesLocal) {
5045     DM cdm = NULL;
5046 
5047     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5048     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
5049     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
5050     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
5051     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
5052   }
5053   *c = dm->coordinates;
5054   PetscFunctionReturn(0);
5055 }
5056 
5057 /*@
5058   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
5059 
5060   Collective on DM
5061 
5062   Input Parameter:
5063 . dm - the DM
5064 
5065   Level: advanced
5066 
5067 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5068 .seealso: DMGetCoordinatesLocalNoncollective()
5069 @*/
5070 PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
5071 {
5072   DM cdm = NULL;
5073   PetscErrorCode ierr;
5074 
5075   PetscFunctionBegin;
5076   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5077   if (!dm->coordinatesLocal && dm->coordinates) {
5078     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5079     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
5080     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
5081     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
5082     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
5083   }
5084   PetscFunctionReturn(0);
5085 }
5086 
5087 /*@
5088   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
5089 
5090   Collective on DM
5091 
5092   Input Parameter:
5093 . dm - the DM
5094 
5095   Output Parameter:
5096 . c - coordinate vector
5097 
5098   Note:
5099   This is a borrowed reference, so the user should NOT destroy this vector
5100 
5101   Each process has the local and ghost coordinates
5102 
5103   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
5104   and (x_0,y_0,z_0,x_1,y_1,z_1...)
5105 
5106   Level: intermediate
5107 
5108 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5109 .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
5110 @*/
5111 PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
5112 {
5113   PetscErrorCode ierr;
5114 
5115   PetscFunctionBegin;
5116   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5117   PetscValidPointer(c,2);
5118   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
5119   *c = dm->coordinatesLocal;
5120   PetscFunctionReturn(0);
5121 }
5122 
5123 /*@
5124   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
5125 
5126   Not collective
5127 
5128   Input Parameter:
5129 . dm - the DM
5130 
5131   Output Parameter:
5132 . c - coordinate vector
5133 
5134   Level: advanced
5135 
5136 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5137 .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
5138 @*/
5139 PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
5140 {
5141   PetscFunctionBegin;
5142   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5143   PetscValidPointer(c,2);
5144   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
5145   *c = dm->coordinatesLocal;
5146   PetscFunctionReturn(0);
5147 }
5148 
5149 /*@
5150   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
5151 
5152   Not collective
5153 
5154   Input Parameter:
5155 + dm - the DM
5156 - p - the IS of points whose coordinates will be returned
5157 
5158   Output Parameter:
5159 + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
5160 - pCoord - the Vec with coordinates of points in p
5161 
5162   Note:
5163   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
5164 
5165   This creates a new vector, so the user SHOULD destroy this vector
5166 
5167   Each process has the local and ghost coordinates
5168 
5169   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
5170   and (x_0,y_0,z_0,x_1,y_1,z_1...)
5171 
5172   Level: advanced
5173 
5174 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5175 .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
5176 @*/
5177 PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
5178 {
5179   PetscSection        cs, newcs;
5180   Vec                 coords;
5181   const PetscScalar   *arr;
5182   PetscScalar         *newarr=NULL;
5183   PetscInt            n;
5184   PetscErrorCode      ierr;
5185 
5186   PetscFunctionBegin;
5187   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5188   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
5189   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
5190   if (pCoord) PetscValidPointer(pCoord, 4);
5191   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
5192   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
5193   cs = dm->coordinateDM->defaultSection;
5194   coords = dm->coordinatesLocal;
5195   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
5196   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
5197   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
5198   if (pCoord) {
5199     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
5200     /* set array in two steps to mimic PETSC_OWN_POINTER */
5201     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
5202     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5203   } else {
5204     ierr = PetscFree(newarr);CHKERRQ(ierr);
5205   }
5206   if (pCoordSection) {*pCoordSection = newcs;}
5207   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
5208   PetscFunctionReturn(0);
5209 }
5210 
5211 PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5212 {
5213   PetscErrorCode ierr;
5214 
5215   PetscFunctionBegin;
5216   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5217   PetscValidPointer(field,2);
5218   if (!dm->coordinateField) {
5219     if (dm->ops->createcoordinatefield) {
5220       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5221     }
5222   }
5223   *field = dm->coordinateField;
5224   PetscFunctionReturn(0);
5225 }
5226 
5227 PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5228 {
5229   PetscErrorCode ierr;
5230 
5231   PetscFunctionBegin;
5232   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5233   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5234   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5235   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5236   dm->coordinateField = field;
5237   PetscFunctionReturn(0);
5238 }
5239 
5240 /*@
5241   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
5242 
5243   Collective on DM
5244 
5245   Input Parameter:
5246 . dm - the DM
5247 
5248   Output Parameter:
5249 . cdm - coordinate DM
5250 
5251   Level: intermediate
5252 
5253 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5254 .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
5255 @*/
5256 PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
5257 {
5258   PetscErrorCode ierr;
5259 
5260   PetscFunctionBegin;
5261   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5262   PetscValidPointer(cdm,2);
5263   if (!dm->coordinateDM) {
5264     DM cdm;
5265 
5266     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5267     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5268     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5269      * until the call to CreateCoordinateDM) */
5270     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5271     dm->coordinateDM = cdm;
5272   }
5273   *cdm = dm->coordinateDM;
5274   PetscFunctionReturn(0);
5275 }
5276 
5277 /*@
5278   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
5279 
5280   Logically Collective on DM
5281 
5282   Input Parameters:
5283 + dm - the DM
5284 - cdm - coordinate DM
5285 
5286   Level: intermediate
5287 
5288 .keywords: distributed array, get, corners, nodes, local indices, coordinates
5289 .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
5290 @*/
5291 PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
5292 {
5293   PetscErrorCode ierr;
5294 
5295   PetscFunctionBegin;
5296   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5297   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5298   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5299   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5300   dm->coordinateDM = cdm;
5301   PetscFunctionReturn(0);
5302 }
5303 
5304 /*@
5305   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
5306 
5307   Not Collective
5308 
5309   Input Parameter:
5310 . dm - The DM object
5311 
5312   Output Parameter:
5313 . dim - The embedding dimension
5314 
5315   Level: intermediate
5316 
5317 .keywords: mesh, coordinates
5318 .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5319 @*/
5320 PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
5321 {
5322   PetscFunctionBegin;
5323   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5324   PetscValidPointer(dim, 2);
5325   if (dm->dimEmbed == PETSC_DEFAULT) {
5326     dm->dimEmbed = dm->dim;
5327   }
5328   *dim = dm->dimEmbed;
5329   PetscFunctionReturn(0);
5330 }
5331 
5332 /*@
5333   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
5334 
5335   Not Collective
5336 
5337   Input Parameters:
5338 + dm  - The DM object
5339 - dim - The embedding dimension
5340 
5341   Level: intermediate
5342 
5343 .keywords: mesh, coordinates
5344 .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5345 @*/
5346 PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
5347 {
5348   PetscDS        ds;
5349   PetscErrorCode ierr;
5350 
5351   PetscFunctionBegin;
5352   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5353   dm->dimEmbed = dim;
5354   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5355   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
5356   PetscFunctionReturn(0);
5357 }
5358 
5359 /*@
5360   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5361 
5362   Collective on DM
5363 
5364   Input Parameter:
5365 . dm - The DM object
5366 
5367   Output Parameter:
5368 . section - The PetscSection object
5369 
5370   Level: intermediate
5371 
5372 .keywords: mesh, coordinates
5373 .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
5374 @*/
5375 PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5376 {
5377   DM             cdm;
5378   PetscErrorCode ierr;
5379 
5380   PetscFunctionBegin;
5381   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5382   PetscValidPointer(section, 2);
5383   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5384   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
5385   PetscFunctionReturn(0);
5386 }
5387 
5388 /*@
5389   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5390 
5391   Not Collective
5392 
5393   Input Parameters:
5394 + dm      - The DM object
5395 . dim     - The embedding dimension, or PETSC_DETERMINE
5396 - section - The PetscSection object
5397 
5398   Level: intermediate
5399 
5400 .keywords: mesh, coordinates
5401 .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
5402 @*/
5403 PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5404 {
5405   DM             cdm;
5406   PetscErrorCode ierr;
5407 
5408   PetscFunctionBegin;
5409   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5410   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5411   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5412   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
5413   if (dim == PETSC_DETERMINE) {
5414     PetscInt d = PETSC_DEFAULT;
5415     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
5416 
5417     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
5418     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
5419     pStart = PetscMax(vStart, pStart);
5420     pEnd   = PetscMin(vEnd, pEnd);
5421     for (v = pStart; v < pEnd; ++v) {
5422       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
5423       if (dd) {d = dd; break;}
5424     }
5425     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
5426   }
5427   PetscFunctionReturn(0);
5428 }
5429 
5430 /*@C
5431   DMGetPeriodicity - Get the description of mesh periodicity
5432 
5433   Input Parameters:
5434 . dm      - The DM object
5435 
5436   Output Parameters:
5437 + per     - Whether the DM is periodic or not
5438 . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
5439 . L       - If we assume the mesh is a torus, this is the length of each coordinate
5440 - bd      - This describes the type of periodicity in each topological dimension
5441 
5442   Level: developer
5443 
5444 .seealso: DMGetPeriodicity()
5445 @*/
5446 PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5447 {
5448   PetscFunctionBegin;
5449   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5450   if (per)     *per     = dm->periodic;
5451   if (L)       *L       = dm->L;
5452   if (maxCell) *maxCell = dm->maxCell;
5453   if (bd)      *bd      = dm->bdtype;
5454   PetscFunctionReturn(0);
5455 }
5456 
5457 /*@C
5458   DMSetPeriodicity - Set the description of mesh periodicity
5459 
5460   Input Parameters:
5461 + dm      - The DM object
5462 . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
5463 . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
5464 . L       - If we assume the mesh is a torus, this is the length of each coordinate
5465 - bd      - This describes the type of periodicity in each topological dimension
5466 
5467   Level: developer
5468 
5469 .seealso: DMGetPeriodicity()
5470 @*/
5471 PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5472 {
5473   PetscInt       dim, d;
5474   PetscErrorCode ierr;
5475 
5476   PetscFunctionBegin;
5477   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5478   PetscValidLogicalCollectiveBool(dm,per,2);
5479   if (maxCell) {
5480     PetscValidPointer(maxCell,3);
5481     PetscValidPointer(L,4);
5482     PetscValidPointer(bd,5);
5483   }
5484   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
5485   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
5486   if (maxCell) {
5487     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
5488     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
5489   }
5490   dm->periodic = per;
5491   PetscFunctionReturn(0);
5492 }
5493 
5494 /*@
5495   DMLocalizeCoordinate - If a mesh is periodic (a torus with lengths L_i, some of which can be infinite), project the coordinate onto [0, L_i) in each dimension.
5496 
5497   Input Parameters:
5498 + dm     - The DM
5499 . in     - The input coordinate point (dim numbers)
5500 - endpoint - Include the endpoint L_i
5501 
5502   Output Parameter:
5503 . out - The localized coordinate point
5504 
5505   Level: developer
5506 
5507 .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
5508 @*/
5509 PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
5510 {
5511   PetscInt       dim, d;
5512   PetscErrorCode ierr;
5513 
5514   PetscFunctionBegin;
5515   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
5516   if (!dm->maxCell) {
5517     for (d = 0; d < dim; ++d) out[d] = in[d];
5518   } else {
5519     if (endpoint) {
5520       for (d = 0; d < dim; ++d) {
5521         if ((PetscAbsReal(PetscRealPart(in[d])/dm->L[d] - PetscFloorReal(PetscRealPart(in[d])/dm->L[d])) < PETSC_SMALL) && (PetscRealPart(in[d])/dm->L[d] > PETSC_SMALL)) {
5522           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
5523         } else {
5524           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
5525         }
5526       }
5527     } else {
5528       for (d = 0; d < dim; ++d) {
5529         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
5530       }
5531     }
5532   }
5533   PetscFunctionReturn(0);
5534 }
5535 
5536 /*
5537   DMLocalizeCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
5538 
5539   Input Parameters:
5540 + dm     - The DM
5541 . dim    - The spatial dimension
5542 . anchor - The anchor point, the input point can be no more than maxCell away from it
5543 - in     - The input coordinate point (dim numbers)
5544 
5545   Output Parameter:
5546 . out - The localized coordinate point
5547 
5548   Level: developer
5549 
5550   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
5551 
5552 .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
5553 */
5554 PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
5555 {
5556   PetscInt d;
5557 
5558   PetscFunctionBegin;
5559   if (!dm->maxCell) {
5560     for (d = 0; d < dim; ++d) out[d] = in[d];
5561   } else {
5562     for (d = 0; d < dim; ++d) {
5563       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
5564         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
5565       } else {
5566         out[d] = in[d];
5567       }
5568     }
5569   }
5570   PetscFunctionReturn(0);
5571 }
5572 PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
5573 {
5574   PetscInt d;
5575 
5576   PetscFunctionBegin;
5577   if (!dm->maxCell) {
5578     for (d = 0; d < dim; ++d) out[d] = in[d];
5579   } else {
5580     for (d = 0; d < dim; ++d) {
5581       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
5582         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
5583       } else {
5584         out[d] = in[d];
5585       }
5586     }
5587   }
5588   PetscFunctionReturn(0);
5589 }
5590 
5591 /*
5592   DMLocalizeAddCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
5593 
5594   Input Parameters:
5595 + dm     - The DM
5596 . dim    - The spatial dimension
5597 . anchor - The anchor point, the input point can be no more than maxCell away from it
5598 . in     - The input coordinate delta (dim numbers)
5599 - out    - The input coordinate point (dim numbers)
5600 
5601   Output Parameter:
5602 . out    - The localized coordinate in + out
5603 
5604   Level: developer
5605 
5606   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
5607 
5608 .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
5609 */
5610 PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
5611 {
5612   PetscInt d;
5613 
5614   PetscFunctionBegin;
5615   if (!dm->maxCell) {
5616     for (d = 0; d < dim; ++d) out[d] += in[d];
5617   } else {
5618     for (d = 0; d < dim; ++d) {
5619       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
5620         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
5621       } else {
5622         out[d] += in[d];
5623       }
5624     }
5625   }
5626   PetscFunctionReturn(0);
5627 }
5628 
5629 /*@
5630   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
5631 
5632   Not collective
5633 
5634   Input Parameter:
5635 . dm - The DM
5636 
5637   Output Parameter:
5638   areLocalized - True if localized
5639 
5640   Level: developer
5641 
5642 .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
5643 @*/
5644 PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
5645 {
5646   DM             cdm;
5647   PetscSection   coordSection;
5648   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
5649   PetscBool      isPlex, alreadyLocalized;
5650   PetscErrorCode ierr;
5651 
5652   PetscFunctionBegin;
5653   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5654   PetscValidPointer(areLocalized, 2);
5655   *areLocalized = PETSC_FALSE;
5656 
5657   /* We need some generic way of refering to cells/vertices */
5658   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5659   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
5660   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
5661   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
5662 
5663   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
5664   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
5665   alreadyLocalized = PETSC_FALSE;
5666   for (c = cStart; c < cEnd; ++c) {
5667     if (c < sStart || c >= sEnd) continue;
5668     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
5669     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
5670   }
5671   *areLocalized = alreadyLocalized;
5672   PetscFunctionReturn(0);
5673 }
5674 
5675 /*@
5676   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
5677 
5678   Collective on dm
5679 
5680   Input Parameter:
5681 . dm - The DM
5682 
5683   Output Parameter:
5684   areLocalized - True if localized
5685 
5686   Level: developer
5687 
5688 .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
5689 @*/
5690 PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
5691 {
5692   PetscBool      localized;
5693   PetscErrorCode ierr;
5694 
5695   PetscFunctionBegin;
5696   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5697   PetscValidPointer(areLocalized, 2);
5698   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
5699   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
5700   PetscFunctionReturn(0);
5701 }
5702 
5703 /*@
5704   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
5705 
5706   Collective on dm
5707 
5708   Input Parameter:
5709 . dm - The DM
5710 
5711   Level: developer
5712 
5713 .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
5714 @*/
5715 PetscErrorCode DMLocalizeCoordinates(DM dm)
5716 {
5717   DM             cdm;
5718   PetscSection   coordSection, cSection;
5719   Vec            coordinates,  cVec;
5720   PetscScalar   *coords, *coords2, *anchor, *localized;
5721   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5722   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
5723   PetscInt       maxHeight = 0, h;
5724   PetscInt       *pStart = NULL, *pEnd = NULL;
5725   PetscErrorCode ierr;
5726 
5727   PetscFunctionBegin;
5728   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5729   if (!dm->periodic) PetscFunctionReturn(0);
5730   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5731   if (alreadyLocalized) PetscFunctionReturn(0);
5732 
5733   /* We need some generic way of refering to cells/vertices */
5734   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
5735   {
5736     PetscBool isplex;
5737 
5738     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
5739     if (isplex) {
5740       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
5741       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
5742       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5743       pEnd = &pStart[maxHeight + 1];
5744       newStart = vStart;
5745       newEnd   = vEnd;
5746       for (h = 0; h <= maxHeight; h++) {
5747         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
5748         newStart = PetscMin(newStart,pStart[h]);
5749         newEnd   = PetscMax(newEnd,pEnd[h]);
5750       }
5751     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
5752   }
5753   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
5754   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
5755   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
5756   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5757   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
5758 
5759   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
5760   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
5761   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
5762   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
5763   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
5764 
5765   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
5766   localized = &anchor[bs];
5767   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
5768   for (h = 0; h <= maxHeight; h++) {
5769     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
5770 
5771     for (c = cStart; c < cEnd; ++c) {
5772       PetscScalar *cellCoords = NULL;
5773       PetscInt     b;
5774 
5775       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
5776       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
5777       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
5778       for (d = 0; d < dof/bs; ++d) {
5779         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
5780         for (b = 0; b < bs; b++) {
5781           if (cellCoords[d*bs + b] != localized[b]) break;
5782         }
5783         if (b < bs) break;
5784       }
5785       if (d < dof/bs) {
5786         if (c >= sStart && c < sEnd) {
5787           PetscInt cdof;
5788 
5789           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
5790           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
5791         }
5792         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
5793         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
5794       }
5795       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
5796     }
5797   }
5798   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
5799   if (alreadyLocalizedGlobal) {
5800     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
5801     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
5802     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5803     PetscFunctionReturn(0);
5804   }
5805   for (v = vStart; v < vEnd; ++v) {
5806     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
5807     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
5808     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
5809   }
5810   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
5811   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5812   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
5813   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
5814   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
5815   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
5816   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5817   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
5818   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
5819   for (v = vStart; v < vEnd; ++v) {
5820     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
5821     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
5822     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
5823     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
5824   }
5825   for (h = 0; h <= maxHeight; h++) {
5826     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
5827 
5828     for (c = cStart; c < cEnd; ++c) {
5829       PetscScalar *cellCoords = NULL;
5830       PetscInt     b, cdof;
5831 
5832       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
5833       if (!cdof) continue;
5834       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
5835       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
5836       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
5837       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
5838       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
5839     }
5840   }
5841   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
5842   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5843   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
5844   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
5845   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
5846   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
5847   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
5848   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
5849   PetscFunctionReturn(0);
5850 }
5851 
5852 /*@
5853   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5854 
5855   Collective on Vec v (see explanation below)
5856 
5857   Input Parameters:
5858 + dm - The DM
5859 . v - The Vec of points
5860 . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
5861 - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5862 
5863   Output Parameter:
5864 + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
5865 - cells - The PetscSF containing the ranks and local indices of the containing points.
5866 
5867 
5868   Level: developer
5869 
5870   Notes:
5871   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
5872   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
5873 
5874   If *cellSF is NULL on input, a PetscSF will be created.
5875   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
5876 
5877   An array that maps each point to its containing cell can be obtained with
5878 
5879 $    const PetscSFNode *cells;
5880 $    PetscInt           nFound;
5881 $    const PetscInt    *found;
5882 $
5883 $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
5884 
5885   Where cells[i].rank is the rank of the cell containing point found[i] (or i if found == NULL), and cells[i].index is
5886   the index of the cell in its rank's local numbering.
5887 
5888 .keywords: point location, mesh
5889 .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
5890 @*/
5891 PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5892 {
5893   PetscErrorCode ierr;
5894 
5895   PetscFunctionBegin;
5896   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5897   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5898   PetscValidPointer(cellSF,4);
5899   if (*cellSF) {
5900     PetscMPIInt result;
5901 
5902     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5903     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
5904     if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
5905   } else {
5906     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
5907   }
5908   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5909   if (dm->ops->locatepoints) {
5910     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
5911   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
5912   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5913   PetscFunctionReturn(0);
5914 }
5915 
5916 /*@
5917   DMGetOutputDM - Retrieve the DM associated with the layout for output
5918 
5919   Collective on dm
5920 
5921   Input Parameter:
5922 . dm - The original DM
5923 
5924   Output Parameter:
5925 . odm - The DM which provides the layout for output
5926 
5927   Level: intermediate
5928 
5929 .seealso: VecView(), DMGetGlobalSection()
5930 @*/
5931 PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
5932 {
5933   PetscSection   section;
5934   PetscBool      hasConstraints, ghasConstraints;
5935   PetscErrorCode ierr;
5936 
5937   PetscFunctionBegin;
5938   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5939   PetscValidPointer(odm,2);
5940   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5941   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5942   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
5943   if (!ghasConstraints) {
5944     *odm = dm;
5945     PetscFunctionReturn(0);
5946   }
5947   if (!dm->dmBC) {
5948     PetscSection newSection, gsection;
5949     PetscSF      sf;
5950 
5951     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
5952     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
5953     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5954     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
5955     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
5956     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
5957     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5958     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
5959     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
5960   }
5961   *odm = dm->dmBC;
5962   PetscFunctionReturn(0);
5963 }
5964 
5965 /*@
5966   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5967 
5968   Input Parameter:
5969 . dm - The original DM
5970 
5971   Output Parameters:
5972 + num - The output sequence number
5973 - val - The output sequence value
5974 
5975   Level: intermediate
5976 
5977   Note: This is intended for output that should appear in sequence, for instance
5978   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5979 
5980 .seealso: VecView()
5981 @*/
5982 PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5983 {
5984   PetscFunctionBegin;
5985   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5986   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5987   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5988   PetscFunctionReturn(0);
5989 }
5990 
5991 /*@
5992   DMSetOutputSequenceNumber - Set the sequence number/value for output
5993 
5994   Input Parameters:
5995 + dm - The original DM
5996 . num - The output sequence number
5997 - val - The output sequence value
5998 
5999   Level: intermediate
6000 
6001   Note: This is intended for output that should appear in sequence, for instance
6002   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6003 
6004 .seealso: VecView()
6005 @*/
6006 PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6007 {
6008   PetscFunctionBegin;
6009   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6010   dm->outputSequenceNum = num;
6011   dm->outputSequenceVal = val;
6012   PetscFunctionReturn(0);
6013 }
6014 
6015 /*@C
6016   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6017 
6018   Input Parameters:
6019 + dm   - The original DM
6020 . name - The sequence name
6021 - num  - The output sequence number
6022 
6023   Output Parameter:
6024 . val  - The output sequence value
6025 
6026   Level: intermediate
6027 
6028   Note: This is intended for output that should appear in sequence, for instance
6029   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6030 
6031 .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6032 @*/
6033 PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6034 {
6035   PetscBool      ishdf5;
6036   PetscErrorCode ierr;
6037 
6038   PetscFunctionBegin;
6039   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6040   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6041   PetscValidPointer(val,4);
6042   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6043   if (ishdf5) {
6044 #if defined(PETSC_HAVE_HDF5)
6045     PetscScalar value;
6046 
6047     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
6048     *val = PetscRealPart(value);
6049 #endif
6050   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6051   PetscFunctionReturn(0);
6052 }
6053 
6054 /*@
6055   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
6056 
6057   Not collective
6058 
6059   Input Parameter:
6060 . dm - The DM
6061 
6062   Output Parameter:
6063 . useNatural - The flag to build the mapping to a natural order during distribution
6064 
6065   Level: beginner
6066 
6067 .seealso: DMSetUseNatural(), DMCreate()
6068 @*/
6069 PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
6070 {
6071   PetscFunctionBegin;
6072   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6073   PetscValidPointer(useNatural, 2);
6074   *useNatural = dm->useNatural;
6075   PetscFunctionReturn(0);
6076 }
6077 
6078 /*@
6079   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
6080 
6081   Collective on dm
6082 
6083   Input Parameters:
6084 + dm - The DM
6085 - useNatural - The flag to build the mapping to a natural order during distribution
6086 
6087   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
6088 
6089   Level: beginner
6090 
6091 .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
6092 @*/
6093 PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
6094 {
6095   PetscFunctionBegin;
6096   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6097   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
6098   dm->useNatural = useNatural;
6099   PetscFunctionReturn(0);
6100 }
6101 
6102 
6103 /*@C
6104   DMCreateLabel - Create a label of the given name if it does not already exist
6105 
6106   Not Collective
6107 
6108   Input Parameters:
6109 + dm   - The DM object
6110 - name - The label name
6111 
6112   Level: intermediate
6113 
6114 .keywords: mesh
6115 .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6116 @*/
6117 PetscErrorCode DMCreateLabel(DM dm, const char name[])
6118 {
6119   DMLabelLink    next  = dm->labels->next;
6120   PetscBool      flg   = PETSC_FALSE;
6121   const char    *lname;
6122   PetscErrorCode ierr;
6123 
6124   PetscFunctionBegin;
6125   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6126   PetscValidCharPointer(name, 2);
6127   while (next) {
6128     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6129     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6130     if (flg) break;
6131     next = next->next;
6132   }
6133   if (!flg) {
6134     DMLabelLink tmpLabel;
6135 
6136     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6137     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
6138     tmpLabel->output = PETSC_TRUE;
6139     tmpLabel->next   = dm->labels->next;
6140     dm->labels->next = tmpLabel;
6141   }
6142   PetscFunctionReturn(0);
6143 }
6144 
6145 /*@C
6146   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6147 
6148   Not Collective
6149 
6150   Input Parameters:
6151 + dm   - The DM object
6152 . name - The label name
6153 - point - The mesh point
6154 
6155   Output Parameter:
6156 . value - The label value for this point, or -1 if the point is not in the label
6157 
6158   Level: beginner
6159 
6160 .keywords: mesh
6161 .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6162 @*/
6163 PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6164 {
6165   DMLabel        label;
6166   PetscErrorCode ierr;
6167 
6168   PetscFunctionBegin;
6169   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6170   PetscValidCharPointer(name, 2);
6171   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6172   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6173   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6174   PetscFunctionReturn(0);
6175 }
6176 
6177 /*@C
6178   DMSetLabelValue - Add a point to a Sieve Label with given value
6179 
6180   Not Collective
6181 
6182   Input Parameters:
6183 + dm   - The DM object
6184 . name - The label name
6185 . point - The mesh point
6186 - value - The label value for this point
6187 
6188   Output Parameter:
6189 
6190   Level: beginner
6191 
6192 .keywords: mesh
6193 .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6194 @*/
6195 PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6196 {
6197   DMLabel        label;
6198   PetscErrorCode ierr;
6199 
6200   PetscFunctionBegin;
6201   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6202   PetscValidCharPointer(name, 2);
6203   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6204   if (!label) {
6205     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6206     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6207   }
6208   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6209   PetscFunctionReturn(0);
6210 }
6211 
6212 /*@C
6213   DMClearLabelValue - Remove a point from a Sieve Label with given value
6214 
6215   Not Collective
6216 
6217   Input Parameters:
6218 + dm   - The DM object
6219 . name - The label name
6220 . point - The mesh point
6221 - value - The label value for this point
6222 
6223   Output Parameter:
6224 
6225   Level: beginner
6226 
6227 .keywords: mesh
6228 .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6229 @*/
6230 PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6231 {
6232   DMLabel        label;
6233   PetscErrorCode ierr;
6234 
6235   PetscFunctionBegin;
6236   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6237   PetscValidCharPointer(name, 2);
6238   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6239   if (!label) PetscFunctionReturn(0);
6240   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6241   PetscFunctionReturn(0);
6242 }
6243 
6244 /*@C
6245   DMGetLabelSize - Get the number of different integer ids in a Label
6246 
6247   Not Collective
6248 
6249   Input Parameters:
6250 + dm   - The DM object
6251 - name - The label name
6252 
6253   Output Parameter:
6254 . size - The number of different integer ids, or 0 if the label does not exist
6255 
6256   Level: beginner
6257 
6258 .keywords: mesh
6259 .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6260 @*/
6261 PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6262 {
6263   DMLabel        label;
6264   PetscErrorCode ierr;
6265 
6266   PetscFunctionBegin;
6267   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6268   PetscValidCharPointer(name, 2);
6269   PetscValidPointer(size, 3);
6270   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6271   *size = 0;
6272   if (!label) PetscFunctionReturn(0);
6273   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6274   PetscFunctionReturn(0);
6275 }
6276 
6277 /*@C
6278   DMGetLabelIdIS - Get the integer ids in a label
6279 
6280   Not Collective
6281 
6282   Input Parameters:
6283 + mesh - The DM object
6284 - name - The label name
6285 
6286   Output Parameter:
6287 . ids - The integer ids, or NULL if the label does not exist
6288 
6289   Level: beginner
6290 
6291 .keywords: mesh
6292 .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6293 @*/
6294 PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6295 {
6296   DMLabel        label;
6297   PetscErrorCode ierr;
6298 
6299   PetscFunctionBegin;
6300   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6301   PetscValidCharPointer(name, 2);
6302   PetscValidPointer(ids, 3);
6303   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6304   *ids = NULL;
6305  if (label) {
6306     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6307   } else {
6308     /* returning an empty IS */
6309     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6310   }
6311   PetscFunctionReturn(0);
6312 }
6313 
6314 /*@C
6315   DMGetStratumSize - Get the number of points in a label stratum
6316 
6317   Not Collective
6318 
6319   Input Parameters:
6320 + dm - The DM object
6321 . name - The label name
6322 - value - The stratum value
6323 
6324   Output Parameter:
6325 . size - The stratum size
6326 
6327   Level: beginner
6328 
6329 .keywords: mesh
6330 .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6331 @*/
6332 PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6333 {
6334   DMLabel        label;
6335   PetscErrorCode ierr;
6336 
6337   PetscFunctionBegin;
6338   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6339   PetscValidCharPointer(name, 2);
6340   PetscValidPointer(size, 4);
6341   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6342   *size = 0;
6343   if (!label) PetscFunctionReturn(0);
6344   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6345   PetscFunctionReturn(0);
6346 }
6347 
6348 /*@C
6349   DMGetStratumIS - Get the points in a label stratum
6350 
6351   Not Collective
6352 
6353   Input Parameters:
6354 + dm - The DM object
6355 . name - The label name
6356 - value - The stratum value
6357 
6358   Output Parameter:
6359 . points - The stratum points, or NULL if the label does not exist or does not have that value
6360 
6361   Level: beginner
6362 
6363 .keywords: mesh
6364 .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6365 @*/
6366 PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6367 {
6368   DMLabel        label;
6369   PetscErrorCode ierr;
6370 
6371   PetscFunctionBegin;
6372   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6373   PetscValidCharPointer(name, 2);
6374   PetscValidPointer(points, 4);
6375   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6376   *points = NULL;
6377   if (!label) PetscFunctionReturn(0);
6378   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6379   PetscFunctionReturn(0);
6380 }
6381 
6382 /*@C
6383   DMSetStratumIS - Set the points in a label stratum
6384 
6385   Not Collective
6386 
6387   Input Parameters:
6388 + dm - The DM object
6389 . name - The label name
6390 . value - The stratum value
6391 - points - The stratum points
6392 
6393   Level: beginner
6394 
6395 .keywords: mesh
6396 .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
6397 @*/
6398 PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
6399 {
6400   DMLabel        label;
6401   PetscErrorCode ierr;
6402 
6403   PetscFunctionBegin;
6404   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6405   PetscValidCharPointer(name, 2);
6406   PetscValidPointer(points, 4);
6407   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6408   if (!label) PetscFunctionReturn(0);
6409   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
6410   PetscFunctionReturn(0);
6411 }
6412 
6413 /*@C
6414   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6415 
6416   Not Collective
6417 
6418   Input Parameters:
6419 + dm   - The DM object
6420 . name - The label name
6421 - value - The label value for this point
6422 
6423   Output Parameter:
6424 
6425   Level: beginner
6426 
6427 .keywords: mesh
6428 .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6429 @*/
6430 PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6431 {
6432   DMLabel        label;
6433   PetscErrorCode ierr;
6434 
6435   PetscFunctionBegin;
6436   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6437   PetscValidCharPointer(name, 2);
6438   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6439   if (!label) PetscFunctionReturn(0);
6440   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6441   PetscFunctionReturn(0);
6442 }
6443 
6444 /*@
6445   DMGetNumLabels - Return the number of labels defined by the mesh
6446 
6447   Not Collective
6448 
6449   Input Parameter:
6450 . dm   - The DM object
6451 
6452   Output Parameter:
6453 . numLabels - the number of Labels
6454 
6455   Level: intermediate
6456 
6457 .keywords: mesh
6458 .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6459 @*/
6460 PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6461 {
6462   DMLabelLink next = dm->labels->next;
6463   PetscInt  n    = 0;
6464 
6465   PetscFunctionBegin;
6466   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6467   PetscValidPointer(numLabels, 2);
6468   while (next) {++n; next = next->next;}
6469   *numLabels = n;
6470   PetscFunctionReturn(0);
6471 }
6472 
6473 /*@C
6474   DMGetLabelName - Return the name of nth label
6475 
6476   Not Collective
6477 
6478   Input Parameters:
6479 + dm - The DM object
6480 - n  - the label number
6481 
6482   Output Parameter:
6483 . name - the label name
6484 
6485   Level: intermediate
6486 
6487 .keywords: mesh
6488 .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6489 @*/
6490 PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6491 {
6492   DMLabelLink    next = dm->labels->next;
6493   PetscInt       l    = 0;
6494   PetscErrorCode ierr;
6495 
6496   PetscFunctionBegin;
6497   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6498   PetscValidPointer(name, 3);
6499   while (next) {
6500     if (l == n) {
6501       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6502       PetscFunctionReturn(0);
6503     }
6504     ++l;
6505     next = next->next;
6506   }
6507   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6508 }
6509 
6510 /*@C
6511   DMHasLabel - Determine whether the mesh has a label of a given name
6512 
6513   Not Collective
6514 
6515   Input Parameters:
6516 + dm   - The DM object
6517 - name - The label name
6518 
6519   Output Parameter:
6520 . hasLabel - PETSC_TRUE if the label is present
6521 
6522   Level: intermediate
6523 
6524 .keywords: mesh
6525 .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6526 @*/
6527 PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6528 {
6529   DMLabelLink    next = dm->labels->next;
6530   const char    *lname;
6531   PetscErrorCode ierr;
6532 
6533   PetscFunctionBegin;
6534   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6535   PetscValidCharPointer(name, 2);
6536   PetscValidPointer(hasLabel, 3);
6537   *hasLabel = PETSC_FALSE;
6538   while (next) {
6539     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6540     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6541     if (*hasLabel) break;
6542     next = next->next;
6543   }
6544   PetscFunctionReturn(0);
6545 }
6546 
6547 /*@C
6548   DMGetLabel - Return the label of a given name, or NULL
6549 
6550   Not Collective
6551 
6552   Input Parameters:
6553 + dm   - The DM object
6554 - name - The label name
6555 
6556   Output Parameter:
6557 . label - The DMLabel, or NULL if the label is absent
6558 
6559   Level: intermediate
6560 
6561 .keywords: mesh
6562 .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6563 @*/
6564 PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6565 {
6566   DMLabelLink    next = dm->labels->next;
6567   PetscBool      hasLabel;
6568   const char    *lname;
6569   PetscErrorCode ierr;
6570 
6571   PetscFunctionBegin;
6572   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6573   PetscValidCharPointer(name, 2);
6574   PetscValidPointer(label, 3);
6575   *label = NULL;
6576   while (next) {
6577     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6578     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6579     if (hasLabel) {
6580       *label = next->label;
6581       break;
6582     }
6583     next = next->next;
6584   }
6585   PetscFunctionReturn(0);
6586 }
6587 
6588 /*@C
6589   DMGetLabelByNum - Return the nth label
6590 
6591   Not Collective
6592 
6593   Input Parameters:
6594 + dm - The DM object
6595 - n  - the label number
6596 
6597   Output Parameter:
6598 . label - the label
6599 
6600   Level: intermediate
6601 
6602 .keywords: mesh
6603 .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6604 @*/
6605 PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6606 {
6607   DMLabelLink next = dm->labels->next;
6608   PetscInt    l    = 0;
6609 
6610   PetscFunctionBegin;
6611   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6612   PetscValidPointer(label, 3);
6613   while (next) {
6614     if (l == n) {
6615       *label = next->label;
6616       PetscFunctionReturn(0);
6617     }
6618     ++l;
6619     next = next->next;
6620   }
6621   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6622 }
6623 
6624 /*@C
6625   DMAddLabel - Add the label to this mesh
6626 
6627   Not Collective
6628 
6629   Input Parameters:
6630 + dm   - The DM object
6631 - label - The DMLabel
6632 
6633   Level: developer
6634 
6635 .keywords: mesh
6636 .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6637 @*/
6638 PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6639 {
6640   DMLabelLink    tmpLabel;
6641   PetscBool      hasLabel;
6642   const char    *lname;
6643   PetscErrorCode ierr;
6644 
6645   PetscFunctionBegin;
6646   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6647   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
6648   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
6649   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
6650   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6651   tmpLabel->label  = label;
6652   tmpLabel->output = PETSC_TRUE;
6653   tmpLabel->next   = dm->labels->next;
6654   dm->labels->next = tmpLabel;
6655   PetscFunctionReturn(0);
6656 }
6657 
6658 /*@C
6659   DMRemoveLabel - Remove the label from this mesh
6660 
6661   Not Collective
6662 
6663   Input Parameters:
6664 + dm   - The DM object
6665 - name - The label name
6666 
6667   Output Parameter:
6668 . label - The DMLabel, or NULL if the label is absent
6669 
6670   Level: developer
6671 
6672 .keywords: mesh
6673 .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6674 @*/
6675 PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6676 {
6677   DMLabelLink    next = dm->labels->next;
6678   DMLabelLink    last = NULL;
6679   PetscBool      hasLabel;
6680   const char    *lname;
6681   PetscErrorCode ierr;
6682 
6683   PetscFunctionBegin;
6684   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6685   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6686   *label = NULL;
6687   if (!hasLabel) PetscFunctionReturn(0);
6688   while (next) {
6689     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6690     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6691     if (hasLabel) {
6692       if (last) last->next       = next->next;
6693       else      dm->labels->next = next->next;
6694       next->next = NULL;
6695       *label     = next->label;
6696       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6697       if (hasLabel) {
6698         dm->depthLabel = NULL;
6699       }
6700       ierr = PetscFree(next);CHKERRQ(ierr);
6701       break;
6702     }
6703     last = next;
6704     next = next->next;
6705   }
6706   PetscFunctionReturn(0);
6707 }
6708 
6709 /*@C
6710   DMGetLabelOutput - Get the output flag for a given label
6711 
6712   Not Collective
6713 
6714   Input Parameters:
6715 + dm   - The DM object
6716 - name - The label name
6717 
6718   Output Parameter:
6719 . output - The flag for output
6720 
6721   Level: developer
6722 
6723 .keywords: mesh
6724 .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6725 @*/
6726 PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6727 {
6728   DMLabelLink    next = dm->labels->next;
6729   const char    *lname;
6730   PetscErrorCode ierr;
6731 
6732   PetscFunctionBegin;
6733   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6734   PetscValidPointer(name, 2);
6735   PetscValidPointer(output, 3);
6736   while (next) {
6737     PetscBool flg;
6738 
6739     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6740     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6741     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6742     next = next->next;
6743   }
6744   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6745 }
6746 
6747 /*@C
6748   DMSetLabelOutput - Set the output flag for a given label
6749 
6750   Not Collective
6751 
6752   Input Parameters:
6753 + dm     - The DM object
6754 . name   - The label name
6755 - output - The flag for output
6756 
6757   Level: developer
6758 
6759 .keywords: mesh
6760 .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6761 @*/
6762 PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6763 {
6764   DMLabelLink    next = dm->labels->next;
6765   const char    *lname;
6766   PetscErrorCode ierr;
6767 
6768   PetscFunctionBegin;
6769   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6770   PetscValidPointer(name, 2);
6771   while (next) {
6772     PetscBool flg;
6773 
6774     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6775     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6776     if (flg) {next->output = output; PetscFunctionReturn(0);}
6777     next = next->next;
6778   }
6779   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6780 }
6781 
6782 
6783 /*@
6784   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6785 
6786   Collective on DM
6787 
6788   Input Parameter:
6789 . dmA - The DM object with initial labels
6790 
6791   Output Parameter:
6792 . dmB - The DM object with copied labels
6793 
6794   Level: intermediate
6795 
6796   Note: This is typically used when interpolating or otherwise adding to a mesh
6797 
6798 .keywords: mesh
6799 .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6800 @*/
6801 PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6802 {
6803   PetscInt       numLabels, l;
6804   PetscErrorCode ierr;
6805 
6806   PetscFunctionBegin;
6807   if (dmA == dmB) PetscFunctionReturn(0);
6808   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6809   for (l = 0; l < numLabels; ++l) {
6810     DMLabel     label, labelNew;
6811     const char *name;
6812     PetscBool   flg;
6813 
6814     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6815     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6816     if (flg) continue;
6817     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
6818     if (flg) continue;
6819     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6820     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6821     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6822   }
6823   PetscFunctionReturn(0);
6824 }
6825 
6826 /*@
6827   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6828 
6829   Input Parameter:
6830 . dm - The DM object
6831 
6832   Output Parameter:
6833 . cdm - The coarse DM
6834 
6835   Level: intermediate
6836 
6837 .seealso: DMSetCoarseDM()
6838 @*/
6839 PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6840 {
6841   PetscFunctionBegin;
6842   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6843   PetscValidPointer(cdm, 2);
6844   *cdm = dm->coarseMesh;
6845   PetscFunctionReturn(0);
6846 }
6847 
6848 /*@
6849   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6850 
6851   Input Parameters:
6852 + dm - The DM object
6853 - cdm - The coarse DM
6854 
6855   Level: intermediate
6856 
6857 .seealso: DMGetCoarseDM()
6858 @*/
6859 PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6860 {
6861   PetscErrorCode ierr;
6862 
6863   PetscFunctionBegin;
6864   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6865   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6866   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6867   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6868   dm->coarseMesh = cdm;
6869   PetscFunctionReturn(0);
6870 }
6871 
6872 /*@
6873   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
6874 
6875   Input Parameter:
6876 . dm - The DM object
6877 
6878   Output Parameter:
6879 . fdm - The fine DM
6880 
6881   Level: intermediate
6882 
6883 .seealso: DMSetFineDM()
6884 @*/
6885 PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
6886 {
6887   PetscFunctionBegin;
6888   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6889   PetscValidPointer(fdm, 2);
6890   *fdm = dm->fineMesh;
6891   PetscFunctionReturn(0);
6892 }
6893 
6894 /*@
6895   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
6896 
6897   Input Parameters:
6898 + dm - The DM object
6899 - fdm - The fine DM
6900 
6901   Level: intermediate
6902 
6903 .seealso: DMGetFineDM()
6904 @*/
6905 PetscErrorCode DMSetFineDM(DM dm, DM fdm)
6906 {
6907   PetscErrorCode ierr;
6908 
6909   PetscFunctionBegin;
6910   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6911   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
6912   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
6913   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
6914   dm->fineMesh = fdm;
6915   PetscFunctionReturn(0);
6916 }
6917 
6918 /*=== DMBoundary code ===*/
6919 
6920 PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6921 {
6922   PetscInt       d;
6923   PetscErrorCode ierr;
6924 
6925   PetscFunctionBegin;
6926   for (d = 0; d < dm->Nds; ++d) {
6927     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
6928   }
6929   PetscFunctionReturn(0);
6930 }
6931 
6932 /*@C
6933   DMAddBoundary - Add a boundary condition to the model
6934 
6935   Input Parameters:
6936 + dm          - The DM, with a PetscDS that matches the problem being constrained
6937 . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6938 . name        - The BC name
6939 . labelname   - The label defining constrained points
6940 . field       - The field to constrain
6941 . numcomps    - The number of constrained field components (0 will constrain all fields)
6942 . comps       - An array of constrained component numbers
6943 . bcFunc      - A pointwise function giving boundary values
6944 . numids      - The number of DMLabel ids for constrained points
6945 . ids         - An array of ids for constrained points
6946 - ctx         - An optional user context for bcFunc
6947 
6948   Options Database Keys:
6949 + -bc_<boundary name> <num> - Overrides the boundary ids
6950 - -bc_<boundary name>_comp <num> - Overrides the boundary components
6951 
6952   Level: developer
6953 
6954 .seealso: DMGetBoundary()
6955 @*/
6956 PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
6957 {
6958   PetscDS        ds;
6959   PetscErrorCode ierr;
6960 
6961   PetscFunctionBegin;
6962   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6963   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6964   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
6965   PetscFunctionReturn(0);
6966 }
6967 
6968 /*@
6969   DMGetNumBoundary - Get the number of registered BC
6970 
6971   Input Parameters:
6972 . dm - The mesh object
6973 
6974   Output Parameters:
6975 . numBd - The number of BC
6976 
6977   Level: intermediate
6978 
6979 .seealso: DMAddBoundary(), DMGetBoundary()
6980 @*/
6981 PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6982 {
6983   PetscDS        ds;
6984   PetscErrorCode ierr;
6985 
6986   PetscFunctionBegin;
6987   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6988   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6989   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
6990   PetscFunctionReturn(0);
6991 }
6992 
6993 /*@C
6994   DMGetBoundary - Get a model boundary condition
6995 
6996   Input Parameters:
6997 + dm          - The mesh object
6998 - bd          - The BC number
6999 
7000   Output Parameters:
7001 + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7002 . name        - The BC name
7003 . labelname   - The label defining constrained points
7004 . field       - The field to constrain
7005 . numcomps    - The number of constrained field components
7006 . comps       - An array of constrained component numbers
7007 . bcFunc      - A pointwise function giving boundary values
7008 . numids      - The number of DMLabel ids for constrained points
7009 . ids         - An array of ids for constrained points
7010 - ctx         - An optional user context for bcFunc
7011 
7012   Options Database Keys:
7013 + -bc_<boundary name> <num> - Overrides the boundary ids
7014 - -bc_<boundary name>_comp <num> - Overrides the boundary components
7015 
7016   Level: developer
7017 
7018 .seealso: DMAddBoundary()
7019 @*/
7020 PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
7021 {
7022   PetscDS        ds;
7023   PetscErrorCode ierr;
7024 
7025   PetscFunctionBegin;
7026   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7027   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7028   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7029   PetscFunctionReturn(0);
7030 }
7031 
7032 static PetscErrorCode DMPopulateBoundary(DM dm)
7033 {
7034   PetscDS        ds;
7035   DMBoundary    *lastnext;
7036   DSBoundary     dsbound;
7037   PetscErrorCode ierr;
7038 
7039   PetscFunctionBegin;
7040   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7041   dsbound = ds->boundary;
7042   if (dm->boundary) {
7043     DMBoundary next = dm->boundary;
7044 
7045     /* quick check to see if the PetscDS has changed */
7046     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
7047     /* the PetscDS has changed: tear down and rebuild */
7048     while (next) {
7049       DMBoundary b = next;
7050 
7051       next = b->next;
7052       ierr = PetscFree(b);CHKERRQ(ierr);
7053     }
7054     dm->boundary = NULL;
7055   }
7056 
7057   lastnext = &(dm->boundary);
7058   while (dsbound) {
7059     DMBoundary dmbound;
7060 
7061     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7062     dmbound->dsboundary = dsbound;
7063     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7064     if (!dmbound->label) {ierr = PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);}
7065     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7066     *lastnext = dmbound;
7067     lastnext = &(dmbound->next);
7068     dsbound = dsbound->next;
7069   }
7070   PetscFunctionReturn(0);
7071 }
7072 
7073 PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7074 {
7075   DMBoundary     b;
7076   PetscErrorCode ierr;
7077 
7078   PetscFunctionBegin;
7079   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7080   PetscValidPointer(isBd, 3);
7081   *isBd = PETSC_FALSE;
7082   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7083   b = dm->boundary;
7084   while (b && !(*isBd)) {
7085     DMLabel    label = b->label;
7086     DSBoundary dsb = b->dsboundary;
7087 
7088     if (label) {
7089       PetscInt i;
7090 
7091       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7092         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7093       }
7094     }
7095     b = b->next;
7096   }
7097   PetscFunctionReturn(0);
7098 }
7099 
7100 /*@C
7101   DMProjectFunction - This projects the given function into the function space provided.
7102 
7103   Input Parameters:
7104 + dm      - The DM
7105 . time    - The time
7106 . funcs   - The coordinate functions to evaluate, one per field
7107 . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
7108 - mode    - The insertion mode for values
7109 
7110   Output Parameter:
7111 . X - vector
7112 
7113    Calling sequence of func:
7114 $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
7115 
7116 +  dim - The spatial dimension
7117 .  x   - The coordinates
7118 .  Nf  - The number of fields
7119 .  u   - The output field values
7120 -  ctx - optional user-defined function context
7121 
7122   Level: developer
7123 
7124 .seealso: DMComputeL2Diff()
7125 @*/
7126 PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7127 {
7128   Vec            localX;
7129   PetscErrorCode ierr;
7130 
7131   PetscFunctionBegin;
7132   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7133   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
7134   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
7135   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
7136   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
7137   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
7138   PetscFunctionReturn(0);
7139 }
7140 
7141 PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7142 {
7143   PetscErrorCode ierr;
7144 
7145   PetscFunctionBegin;
7146   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7147   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
7148   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
7149   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
7150   PetscFunctionReturn(0);
7151 }
7152 
7153 PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
7154 {
7155   Vec            localX;
7156   PetscErrorCode ierr;
7157 
7158   PetscFunctionBegin;
7159   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7160   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
7161   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
7162   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
7163   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
7164   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
7165   PetscFunctionReturn(0);
7166 }
7167 
7168 PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
7169 {
7170   PetscErrorCode ierr;
7171 
7172   PetscFunctionBegin;
7173   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7174   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
7175   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
7176   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
7177   PetscFunctionReturn(0);
7178 }
7179 
7180 PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
7181                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
7182                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7183                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7184                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
7185                                    InsertMode mode, Vec localX)
7186 {
7187   PetscErrorCode ierr;
7188 
7189   PetscFunctionBegin;
7190   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7191   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
7192   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
7193   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
7194   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
7195   PetscFunctionReturn(0);
7196 }
7197 
7198 PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
7199                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
7200                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7201                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7202                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
7203                                         InsertMode mode, Vec localX)
7204 {
7205   PetscErrorCode ierr;
7206 
7207   PetscFunctionBegin;
7208   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7209   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
7210   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
7211   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
7212   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
7213   PetscFunctionReturn(0);
7214 }
7215 
7216 /*@C
7217   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
7218 
7219   Input Parameters:
7220 + dm    - The DM
7221 . time  - The time
7222 . funcs - The functions to evaluate for each field component
7223 . ctxs  - Optional array of contexts to pass to each function, or NULL.
7224 - X     - The coefficient vector u_h, a global vector
7225 
7226   Output Parameter:
7227 . diff - The diff ||u - u_h||_2
7228 
7229   Level: developer
7230 
7231 .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
7232 @*/
7233 PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
7234 {
7235   PetscErrorCode ierr;
7236 
7237   PetscFunctionBegin;
7238   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7239   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7240   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
7241   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
7242   PetscFunctionReturn(0);
7243 }
7244 
7245 /*@C
7246   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7247 
7248   Input Parameters:
7249 + dm    - The DM
7250 , time  - The time
7251 . funcs - The gradient functions to evaluate for each field component
7252 . ctxs  - Optional array of contexts to pass to each function, or NULL.
7253 . X     - The coefficient vector u_h, a global vector
7254 - n     - The vector to project along
7255 
7256   Output Parameter:
7257 . diff - The diff ||(grad u - grad u_h) . n||_2
7258 
7259   Level: developer
7260 
7261 .seealso: DMProjectFunction(), DMComputeL2Diff()
7262 @*/
7263 PetscErrorCode DMComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
7264 {
7265   PetscErrorCode ierr;
7266 
7267   PetscFunctionBegin;
7268   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7269   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7270   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7271   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7272   PetscFunctionReturn(0);
7273 }
7274 
7275 /*@C
7276   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
7277 
7278   Input Parameters:
7279 + dm    - The DM
7280 . time  - The time
7281 . funcs - The functions to evaluate for each field component
7282 . ctxs  - Optional array of contexts to pass to each function, or NULL.
7283 - X     - The coefficient vector u_h, a global vector
7284 
7285   Output Parameter:
7286 . diff - The array of differences, ||u^f - u^f_h||_2
7287 
7288   Level: developer
7289 
7290 .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
7291 @*/
7292 PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
7293 {
7294   PetscErrorCode ierr;
7295 
7296   PetscFunctionBegin;
7297   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7298   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7299   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
7300   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
7301   PetscFunctionReturn(0);
7302 }
7303 
7304 /*@C
7305   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7306                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7307 
7308   Collective on dm
7309 
7310   Input parameters:
7311 + dm - the pre-adaptation DM object
7312 - label - label with the flags
7313 
7314   Output parameters:
7315 . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7316 
7317   Level: intermediate
7318 
7319 .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7320 @*/
7321 PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7322 {
7323   PetscErrorCode ierr;
7324 
7325   PetscFunctionBegin;
7326   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7327   PetscValidPointer(label,2);
7328   PetscValidPointer(dmAdapt,3);
7329   *dmAdapt = NULL;
7330   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
7331   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
7332   PetscFunctionReturn(0);
7333 }
7334 
7335 /*@C
7336   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
7337 
7338   Input Parameters:
7339 + dm - The DM object
7340 . metric - The metric to which the mesh is adapted, defined vertex-wise.
7341 - bdLabel - Label for boundary tags, which will be preserved in the output mesh. bdLabel should be NULL if there is no such label, and should be different from "_boundary_".
7342 
7343   Output Parameter:
7344 . dmAdapt  - Pointer to the DM object containing the adapted mesh
7345 
7346   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
7347 
7348   Level: advanced
7349 
7350 .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
7351 @*/
7352 PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
7353 {
7354   PetscErrorCode ierr;
7355 
7356   PetscFunctionBegin;
7357   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7358   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
7359   if (bdLabel) PetscValidPointer(bdLabel, 3);
7360   PetscValidPointer(dmAdapt, 4);
7361   *dmAdapt = NULL;
7362   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
7363   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7364   PetscFunctionReturn(0);
7365 }
7366 
7367 /*@C
7368  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7369 
7370  Not Collective
7371 
7372  Input Parameter:
7373  . dm    - The DM
7374 
7375  Output Parameter:
7376  . nranks - the number of neighbours
7377  . ranks - the neighbors ranks
7378 
7379  Notes:
7380  Do not free the array, it is freed when the DM is destroyed.
7381 
7382  Level: beginner
7383 
7384  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
7385 @*/
7386 PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7387 {
7388   PetscErrorCode ierr;
7389 
7390   PetscFunctionBegin;
7391   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7392   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7393   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7394   PetscFunctionReturn(0);
7395 }
7396 
7397 #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7398 
7399 /*
7400     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7401     This has be a different function because it requires DM which is not defined in the Mat library
7402 */
7403 PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7404 {
7405   PetscErrorCode ierr;
7406 
7407   PetscFunctionBegin;
7408   if (coloring->ctype == IS_COLORING_LOCAL) {
7409     Vec x1local;
7410     DM  dm;
7411     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7412     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7413     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7414     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7415     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7416     x1   = x1local;
7417   }
7418   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7419   if (coloring->ctype == IS_COLORING_LOCAL) {
7420     DM  dm;
7421     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7422     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7423   }
7424   PetscFunctionReturn(0);
7425 }
7426 
7427 /*@
7428     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7429 
7430     Input Parameter:
7431 .    coloring - the MatFDColoring object
7432 
7433     Developer Notes:
7434     this routine exists because the PETSc Mat library does not know about the DM objects
7435 
7436     Level: advanced
7437 
7438 .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7439 @*/
7440 PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7441 {
7442   PetscFunctionBegin;
7443   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7444   PetscFunctionReturn(0);
7445 }
7446 
7447 /*@
7448     DMGetCompatibility - determine if two DMs are compatible
7449 
7450     Collective
7451 
7452     Input Parameters:
7453 +    dm - the first DM
7454 -    dm2 - the second DM
7455 
7456     Output Parameters:
7457 +    compatible - whether or not the two DMs are compatible
7458 -    set - whether or not the compatible value was set
7459 
7460     Notes:
7461     Two DMs are deemed compatible if they represent the same parallel decomposition
7462     of the same topology. This implies that the the section (field data) on one
7463     "makes sense" with respect to the topology and parallel decomposition of the other.
7464     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
7465     decomposition, with different data.
7466 
7467     Typically, one would confirm compatibility if intending to simultaneously iterate
7468     over a pair of vectors obtained from different DMs.
7469 
7470     For example, two DMDA objects are compatible if they have the same local
7471     and global sizes and the same stencil width. They can have different numbers
7472     of degrees of freedom per node. Thus, one could use the node numbering from
7473     either DM in bounds for a loop over vectors derived from either DM.
7474 
7475     Consider the operation of summing data living on a 2-dof DMDA to data living
7476     on a 1-dof DMDA, which should be compatible, as in the following snippet.
7477 .vb
7478   ...
7479   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
7480   if (set && compatible)  {
7481     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
7482     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
7483     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
7484     for (j=y; j<y+n; ++j) {
7485       for (i=x; i<x+m, ++i) {
7486         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
7487       }
7488     }
7489     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
7490     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
7491   } else {
7492     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
7493   }
7494   ...
7495 .ve
7496 
7497     Checking compatibility might be expensive for a given implementation of DM,
7498     or might be impossible to unambiguously confirm or deny. For this reason,
7499     this function may decline to determine compatibility, and hence users should
7500     always check the "set" output parameter.
7501 
7502     A DM is always compatible with itself.
7503 
7504     In the current implementation, DMs which live on "unequal" communicators
7505     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
7506     incompatible.
7507 
7508     This function is labeled "Collective," as information about all subdomains
7509     is required on each rank. However, in DM implementations which store all this
7510     information locally, this function may be merely "Logically Collective".
7511 
7512     Developer Notes:
7513     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
7514     the DM B is compatible with DM A. Thus, this function checks the implementations
7515     of both dm and dm2 (if they are of different types), attempting to determine
7516     compatibility. It is left to DM implementers to ensure that symmetry is
7517     preserved. The simplest way to do this is, when implementing type-specific
7518     logic for this function, to check for existing logic in the implementation
7519     of other DM types and let *set = PETSC_FALSE if found; the logic of this
7520     function will then call that logic.
7521 
7522     Level: advanced
7523 
7524 .seealso: DM, DMDACreateCompatibleDMDA()
7525 @*/
7526 
7527 PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
7528 {
7529   PetscErrorCode ierr;
7530   PetscMPIInt    compareResult;
7531   DMType         type,type2;
7532   PetscBool      sameType;
7533 
7534   PetscFunctionBegin;
7535   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7536   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
7537 
7538   /* Declare a DM compatible with itself */
7539   if (dm == dm2) {
7540     *set = PETSC_TRUE;
7541     *compatible = PETSC_TRUE;
7542     PetscFunctionReturn(0);
7543   }
7544 
7545   /* Declare a DM incompatible with a DM that lives on an "unequal"
7546      communicator. Note that this does not preclude compatibility with
7547      DMs living on "congruent" or "similar" communicators, but this must be
7548      determined by the implementation-specific logic */
7549   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
7550   if (compareResult == MPI_UNEQUAL) {
7551     *set = PETSC_TRUE;
7552     *compatible = PETSC_FALSE;
7553     PetscFunctionReturn(0);
7554   }
7555 
7556   /* Pass to the implementation-specific routine, if one exists. */
7557   if (dm->ops->getcompatibility) {
7558     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
7559     if (*set) {
7560       PetscFunctionReturn(0);
7561     }
7562   }
7563 
7564   /* If dm and dm2 are of different types, then attempt to check compatibility
7565      with an implementation of this function from dm2 */
7566   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
7567   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
7568   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
7569   if (!sameType && dm2->ops->getcompatibility) {
7570     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
7571   } else {
7572     *set = PETSC_FALSE;
7573   }
7574   PetscFunctionReturn(0);
7575 }
7576