Actual source code: dmshell.c

petsc-main 2021-03-04
Report Typos and Errors
  1: #include <petscdmshell.h>
  2: #include <petscmat.h>
  3: #include <petsc/private/dmimpl.h>

  5: typedef struct  {
  6:   Vec        Xglobal;
  7:   Vec        Xlocal;
  8:   Mat        A;
  9:   VecScatter gtol;
 10:   VecScatter ltog;
 11:   VecScatter ltol;
 12:   void       *ctx;
 13: } DM_Shell;

 15: /*@
 16:    DMGlobalToLocalBeginDefaultShell - Uses the GlobalToLocal VecScatter context set by the user to begin a global to local scatter
 17:    Collective

 19:    Input Arguments:
 20: +  dm - shell DM
 21: .  g - global vector
 22: .  mode - InsertMode
 23: -  l - local vector

 25:    Level: advanced

 27:    Note:  This is not normally called directly by user code, generally user code calls DMGlobalToLocalBegin() and DMGlobalToLocalEnd(). If the user provides their own custom routines to DMShellSetLocalToGlobal() then those routines might have reason to call this function.

 29: .seealso: DMGlobalToLocalEndDefaultShell()
 30: @*/
 31: PetscErrorCode DMGlobalToLocalBeginDefaultShell(DM dm,Vec g,InsertMode mode,Vec l)
 32: {
 34:   DM_Shell       *shell = (DM_Shell*)dm->data;

 37:   if (!shell->gtol) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetGlobalToLocalVecScatter()");
 38:   VecScatterBegin(shell->gtol,g,l,mode,SCATTER_FORWARD);
 39:   return(0);
 40: }

 42: /*@
 43:    DMGlobalToLocalEndDefaultShell - Uses the GlobalToLocal VecScatter context set by the user to end a global to local scatter
 44:    Collective

 46:    Input Arguments:
 47: +  dm - shell DM
 48: .  g - global vector
 49: .  mode - InsertMode
 50: -  l - local vector

 52:    Level: advanced

 54: .seealso: DMGlobalToLocalBeginDefaultShell()
 55: @*/
 56: PetscErrorCode DMGlobalToLocalEndDefaultShell(DM dm,Vec g,InsertMode mode,Vec l)
 57: {
 59:   DM_Shell       *shell = (DM_Shell*)dm->data;

 62:    if (!shell->gtol) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetGlobalToLocalVecScatter()");
 63:   VecScatterEnd(shell->gtol,g,l,mode,SCATTER_FORWARD);
 64:   return(0);
 65: }

 67: /*@
 68:    DMLocalToGlobalBeginDefaultShell - Uses the LocalToGlobal VecScatter context set by the user to begin a local to global scatter
 69:    Collective

 71:    Input Arguments:
 72: +  dm - shell DM
 73: .  l - local vector
 74: .  mode - InsertMode
 75: -  g - global vector

 77:    Level: advanced

 79:    Note:  This is not normally called directly by user code, generally user code calls DMLocalToGlobalBegin() and DMLocalToGlobalEnd(). If the user provides their own custom routines to DMShellSetLocalToGlobal() then those routines might have reason to call this function.

 81: .seealso: DMLocalToGlobalEndDefaultShell()
 82: @*/
 83: PetscErrorCode DMLocalToGlobalBeginDefaultShell(DM dm,Vec l,InsertMode mode,Vec g)
 84: {
 86:   DM_Shell       *shell = (DM_Shell*)dm->data;

 89:   if (!shell->ltog) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetLocalToGlobalVecScatter()");
 90:   VecScatterBegin(shell->ltog,l,g,mode,SCATTER_FORWARD);
 91:   return(0);
 92: }

 94: /*@
 95:    DMLocalToGlobalEndDefaultShell - Uses the LocalToGlobal VecScatter context set by the user to end a local to global scatter
 96:    Collective

 98:    Input Arguments:
 99: +  dm - shell DM
100: .  l - local vector
101: .  mode - InsertMode
102: -  g - global vector

104:    Level: advanced

106: .seealso: DMLocalToGlobalBeginDefaultShell()
107: @*/
108: PetscErrorCode DMLocalToGlobalEndDefaultShell(DM dm,Vec l,InsertMode mode,Vec g)
109: {
111:   DM_Shell       *shell = (DM_Shell*)dm->data;

114:    if (!shell->ltog) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetLocalToGlobalVecScatter()");
115:   VecScatterEnd(shell->ltog,l,g,mode,SCATTER_FORWARD);
116:   return(0);
117: }

119: /*@
120:    DMLocalToLocalBeginDefaultShell - Uses the LocalToLocal VecScatter context set by the user to begin a local to local scatter
121:    Collective

123:    Input Arguments:
124: +  dm - shell DM
125: .  g - the original local vector
126: -  mode - InsertMode

128:    Output Parameter:
129: .  l  - the local vector with correct ghost values

131:    Level: advanced

133:    Note:  This is not normally called directly by user code, generally user code calls DMLocalToLocalBegin() and DMLocalToLocalEnd(). If the user provides their own custom routines to DMShellSetLocalToLocal() then those routines might have reason to call this function.

135: .seealso: DMLocalToLocalEndDefaultShell()
136: @*/
137: PetscErrorCode DMLocalToLocalBeginDefaultShell(DM dm,Vec g,InsertMode mode,Vec l)
138: {
140:   DM_Shell       *shell = (DM_Shell*)dm->data;

143:   if (!shell->ltol) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetLocalToLocalVecScatter()");
144:   VecScatterBegin(shell->ltol,g,l,mode,SCATTER_FORWARD);
145:   return(0);
146: }

148: /*@
149:    DMLocalToLocalEndDefaultShell - Uses the LocalToLocal VecScatter context set by the user to end a local to local scatter
150:    Collective

152:    Input Arguments:
153: +  dm - shell DM
154: .  g - the original local vector
155: -  mode - InsertMode

157:    Output Parameter:
158: .  l  - the local vector with correct ghost values

160:    Level: advanced

162: .seealso: DMLocalToLocalBeginDefaultShell()
163: @*/
164: PetscErrorCode DMLocalToLocalEndDefaultShell(DM dm,Vec g,InsertMode mode,Vec l)
165: {
167:   DM_Shell       *shell = (DM_Shell*)dm->data;

170:    if (!shell->ltol) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_WRONGSTATE, "Cannot be used without first setting the scatter context via DMShellSetGlobalToLocalVecScatter()");
171:   VecScatterEnd(shell->ltol,g,l,mode,SCATTER_FORWARD);
172:   return(0);
173: }

175: static PetscErrorCode DMCreateMatrix_Shell(DM dm,Mat *J)
176: {
178:   DM_Shell       *shell = (DM_Shell*)dm->data;
179:   Mat            A;

184:   if (!shell->A) {
185:     if (shell->Xglobal) {
186:       PetscInt m,M;
187:       PetscInfo(dm,"Naively creating matrix using global vector distribution without preallocation\n");
188:       VecGetSize(shell->Xglobal,&M);
189:       VecGetLocalSize(shell->Xglobal,&m);
190:       MatCreate(PetscObjectComm((PetscObject)dm),&shell->A);
191:       MatSetSizes(shell->A,m,m,M,M);
192:       MatSetType(shell->A,dm->mattype);
193:       MatSetUp(shell->A);
194:     } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Must call DMShellSetMatrix(), DMShellSetCreateMatrix(), or provide a vector");
195:   }
196:   A = shell->A;
197:   MatDuplicate(A,MAT_SHARE_NONZERO_PATTERN,J);
198:   MatSetDM(*J,dm);
199:   return(0);
200: }

202: PetscErrorCode DMCreateGlobalVector_Shell(DM dm,Vec *gvec)
203: {
205:   DM_Shell       *shell = (DM_Shell*)dm->data;
206:   Vec            X;

211:   *gvec = NULL;
212:   X     = shell->Xglobal;
213:   if (!X) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Must call DMShellSetGlobalVector() or DMShellSetCreateGlobalVector()");
214:   /* Need to create a copy in order to attach the DM to the vector */
215:   VecDuplicate(X,gvec);
216:   VecZeroEntries(*gvec);
217:   VecSetDM(*gvec,dm);
218:   return(0);
219: }

221: PetscErrorCode DMCreateLocalVector_Shell(DM dm,Vec *gvec)
222: {
224:   DM_Shell       *shell = (DM_Shell*)dm->data;
225:   Vec            X;

230:   *gvec = NULL;
231:   X     = shell->Xlocal;
232:   if (!X) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_USER,"Must call DMShellSetLocalVector() or DMShellSetCreateLocalVector()");
233:   /* Need to create a copy in order to attach the DM to the vector */
234:   VecDuplicate(X,gvec);
235:   VecZeroEntries(*gvec);
236:   VecSetDM(*gvec,dm);
237:   return(0);
238: }

240: /*@
241:    DMShellSetContext - set some data to be usable by this DM

243:    Collective

245:    Input Arguments:
246: +  dm - shell DM
247: -  ctx - the context

249:    Level: advanced

251: .seealso: DMCreateMatrix(), DMShellGetContext()
252: @*/
253: PetscErrorCode DMShellSetContext(DM dm,void *ctx)
254: {
255:   DM_Shell       *shell = (DM_Shell*)dm->data;
257:   PetscBool      isshell;

261:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
262:   if (!isshell) return(0);
263:   shell->ctx = ctx;
264:   return(0);
265: }

267: /*@
268:    DMShellGetContext - Returns the user-provided context associated to the DM

270:    Collective

272:    Input Argument:
273: .  dm - shell DM

275:    Output Argument:
276: .  ctx - the context

278:    Level: advanced

280: .seealso: DMCreateMatrix(), DMShellSetContext()
281: @*/
282: PetscErrorCode DMShellGetContext(DM dm,void **ctx)
283: {
284:   DM_Shell       *shell = (DM_Shell*)dm->data;
286:   PetscBool      isshell;

290:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
291:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
292:   *ctx = shell->ctx;
293:   return(0);
294: }

296: /*@
297:    DMShellSetMatrix - sets a template matrix associated with the DMShell

299:    Collective

301:    Input Arguments:
302: +  dm - shell DM
303: -  J - template matrix

305:    Level: advanced

307:    Developer Notes:
308:     To avoid circular references, if J is already associated to the same DM, then MatDuplicate(SHARE_NONZERO_PATTERN) is called, followed by removing the DM reference from the private template.

310: .seealso: DMCreateMatrix(), DMShellSetCreateMatrix(), DMShellSetContext(), DMShellGetContext()
311: @*/
312: PetscErrorCode DMShellSetMatrix(DM dm,Mat J)
313: {
314:   DM_Shell       *shell = (DM_Shell*)dm->data;
316:   PetscBool      isshell;
317:   DM             mdm;

322:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
323:   if (!isshell) return(0);
324:   if (J == shell->A) return(0);
325:   MatGetDM(J,&mdm);
326:   PetscObjectReference((PetscObject)J);
327:   MatDestroy(&shell->A);
328:   if (mdm == dm) {
329:     MatDuplicate(J,MAT_SHARE_NONZERO_PATTERN,&shell->A);
330:     MatSetDM(shell->A,NULL);
331:   } else shell->A = J;
332:   return(0);
333: }

335: /*@C
336:    DMShellSetCreateMatrix - sets the routine to create a matrix associated with the shell DM

338:    Logically Collective on dm

340:    Input Arguments:
341: +  dm - the shell DM
342: -  func - the function to create a matrix

344:    Level: advanced

346: .seealso: DMCreateMatrix(), DMShellSetMatrix(), DMShellSetContext(), DMShellGetContext()
347: @*/
348: PetscErrorCode DMShellSetCreateMatrix(DM dm,PetscErrorCode (*func)(DM,Mat*))
349: {
352:   dm->ops->creatematrix = func;
353:   return(0);
354: }

356: /*@
357:    DMShellSetGlobalVector - sets a template global vector associated with the DMShell

359:    Logically Collective on dm

361:    Input Arguments:
362: +  dm - shell DM
363: -  X - template vector

365:    Level: advanced

367: .seealso: DMCreateGlobalVector(), DMShellSetMatrix(), DMShellSetCreateGlobalVector()
368: @*/
369: PetscErrorCode DMShellSetGlobalVector(DM dm,Vec X)
370: {
371:   DM_Shell       *shell = (DM_Shell*)dm->data;
373:   PetscBool      isshell;
374:   DM             vdm;

379:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
380:   if (!isshell) return(0);
381:   VecGetDM(X,&vdm);
382:   /*
383:       if the vector proposed as the new base global vector for the DM is a DM vector associated
384:       with the same DM then the current base global vector for the DM is ok and if we replace it with the new one
385:       we get a circular dependency that prevents the DM from being destroy when it should be.
386:       This occurs when SNESSet/GetNPC() is used with a SNES that does not have a user provided
387:       DM attached to it since the inner SNES (which shares the DM with the outer SNES) tries
388:       to set its input vector (which is associated with the DM) as the base global vector.
389:       Thanks to Juan P. Mendez Granado Re: [petsc-maint] Nonlinear conjugate gradien
390:       for pointing out the problem.
391:    */
392:   if (vdm == dm) return(0);
393:   PetscObjectReference((PetscObject)X);
394:   VecDestroy(&shell->Xglobal);
395:   shell->Xglobal = X;
396:   return(0);
397: }

399: /*@C
400:    DMShellSetCreateGlobalVector - sets the routine to create a global vector associated with the shell DM

402:    Logically Collective

404:    Input Arguments:
405: +  dm - the shell DM
406: -  func - the creation routine

408:    Level: advanced

410: .seealso: DMShellSetGlobalVector(), DMShellSetCreateMatrix(), DMShellSetContext(), DMShellGetContext()
411: @*/
412: PetscErrorCode DMShellSetCreateGlobalVector(DM dm,PetscErrorCode (*func)(DM,Vec*))
413: {
416:   dm->ops->createglobalvector = func;
417:   return(0);
418: }

420: /*@
421:    DMShellSetLocalVector - sets a template local vector associated with the DMShell

423:    Logically Collective on dm

425:    Input Arguments:
426: +  dm - shell DM
427: -  X - template vector

429:    Level: advanced

431: .seealso: DMCreateLocalVector(), DMShellSetMatrix(), DMShellSetCreateLocalVector()
432: @*/
433: PetscErrorCode DMShellSetLocalVector(DM dm,Vec X)
434: {
435:   DM_Shell       *shell = (DM_Shell*)dm->data;
437:   PetscBool      isshell;
438:   DM             vdm;

443:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
444:   if (!isshell) return(0);
445:   VecGetDM(X,&vdm);
446:   /*
447:       if the vector proposed as the new base global vector for the DM is a DM vector associated
448:       with the same DM then the current base global vector for the DM is ok and if we replace it with the new one
449:       we get a circular dependency that prevents the DM from being destroy when it should be.
450:       This occurs when SNESSet/GetNPC() is used with a SNES that does not have a user provided
451:       DM attached to it since the inner SNES (which shares the DM with the outer SNES) tries
452:       to set its input vector (which is associated with the DM) as the base global vector.
453:       Thanks to Juan P. Mendez Granado Re: [petsc-maint] Nonlinear conjugate gradien
454:       for pointing out the problem.
455:    */
456:   if (vdm == dm) return(0);
457:   PetscObjectReference((PetscObject)X);
458:   VecDestroy(&shell->Xlocal);
459:   shell->Xlocal = X;
460:   return(0);
461: }

463: /*@C
464:    DMShellSetCreateLocalVector - sets the routine to create a local vector associated with the shell DM

466:    Logically Collective

468:    Input Arguments:
469: +  dm - the shell DM
470: -  func - the creation routine

472:    Level: advanced

474: .seealso: DMShellSetLocalVector(), DMShellSetCreateMatrix(), DMShellSetContext(), DMShellGetContext()
475: @*/
476: PetscErrorCode DMShellSetCreateLocalVector(DM dm,PetscErrorCode (*func)(DM,Vec*))
477: {
480:   dm->ops->createlocalvector = func;
481:   return(0);
482: }

484: /*@C
485:    DMShellSetGlobalToLocal - Sets the routines used to perform a global to local scatter

487:    Logically Collective on dm

489:    Input Arguments
490: +  dm - the shell DM
491: .  begin - the routine that begins the global to local scatter
492: -  end - the routine that ends the global to local scatter

494:    Notes:
495:     If these functions are not provided but DMShellSetGlobalToLocalVecScatter() is called then
496:    DMGlobalToLocalBeginDefaultShell()/DMGlobalToLocalEndDefaultShell() are used to to perform the transfers

498:    Level: advanced

500: .seealso: DMShellSetLocalToGlobal(), DMGlobalToLocalBeginDefaultShell(), DMGlobalToLocalEndDefaultShell()
501: @*/
502: PetscErrorCode DMShellSetGlobalToLocal(DM dm,PetscErrorCode (*begin)(DM,Vec,InsertMode,Vec),PetscErrorCode (*end)(DM,Vec,InsertMode,Vec))
503: {
506:   dm->ops->globaltolocalbegin = begin;
507:   dm->ops->globaltolocalend = end;
508:   return(0);
509: }

511: /*@C
512:    DMShellSetLocalToGlobal - Sets the routines used to perform a local to global scatter

514:    Logically Collective on dm

516:    Input Arguments
517: +  dm - the shell DM
518: .  begin - the routine that begins the local to global scatter
519: -  end - the routine that ends the local to global scatter

521:    Notes:
522:     If these functions are not provided but DMShellSetLocalToGlobalVecScatter() is called then
523:    DMLocalToGlobalBeginDefaultShell()/DMLocalToGlobalEndDefaultShell() are used to to perform the transfers

525:    Level: advanced

527: .seealso: DMShellSetGlobalToLocal()
528: @*/
529: PetscErrorCode DMShellSetLocalToGlobal(DM dm,PetscErrorCode (*begin)(DM,Vec,InsertMode,Vec),PetscErrorCode (*end)(DM,Vec,InsertMode,Vec))
530: {
533:   dm->ops->localtoglobalbegin = begin;
534:   dm->ops->localtoglobalend = end;
535:   return(0);
536: }

538: /*@C
539:    DMShellSetLocalToLocal - Sets the routines used to perform a local to local scatter

541:    Logically Collective on dm

543:    Input Arguments
544: +  dm - the shell DM
545: .  begin - the routine that begins the local to local scatter
546: -  end - the routine that ends the local to local scatter

548:    Notes:
549:     If these functions are not provided but DMShellSetLocalToLocalVecScatter() is called then
550:    DMLocalToLocalBeginDefaultShell()/DMLocalToLocalEndDefaultShell() are used to to perform the transfers

552:    Level: advanced

554: .seealso: DMShellSetGlobalToLocal(), DMLocalToLocalBeginDefaultShell(), DMLocalToLocalEndDefaultShell()
555: @*/
556: PetscErrorCode DMShellSetLocalToLocal(DM dm,PetscErrorCode (*begin)(DM,Vec,InsertMode,Vec),PetscErrorCode (*end)(DM,Vec,InsertMode,Vec))
557: {
560:   dm->ops->localtolocalbegin = begin;
561:   dm->ops->localtolocalend = end;
562:   return(0);
563: }

565: /*@
566:    DMShellSetGlobalToLocalVecScatter - Sets a VecScatter context for global to local communication

568:    Logically Collective on dm

570:    Input Arguments
571: +  dm - the shell DM
572: -  gtol - the global to local VecScatter context

574:    Level: advanced

576: .seealso: DMShellSetGlobalToLocal(), DMGlobalToLocalBeginDefaultShell(), DMGlobalToLocalEndDefaultShell()
577: @*/
578: PetscErrorCode DMShellSetGlobalToLocalVecScatter(DM dm, VecScatter gtol)
579: {
580:   DM_Shell       *shell = (DM_Shell*)dm->data;

586:   PetscObjectReference((PetscObject)gtol);
587:   VecScatterDestroy(&shell->gtol);
588:   shell->gtol = gtol;
589:   return(0);
590: }

592: /*@
593:    DMShellSetLocalToGlobalVecScatter - Sets a VecScatter context for local to global communication

595:    Logically Collective on dm

597:    Input Arguments
598: +  dm - the shell DM
599: -  ltog - the local to global VecScatter context

601:    Level: advanced

603: .seealso: DMShellSetLocalToGlobal(), DMLocalToGlobalBeginDefaultShell(), DMLocalToGlobalEndDefaultShell()
604: @*/
605: PetscErrorCode DMShellSetLocalToGlobalVecScatter(DM dm, VecScatter ltog)
606: {
607:   DM_Shell       *shell = (DM_Shell*)dm->data;

613:   PetscObjectReference((PetscObject)ltog);
614:   VecScatterDestroy(&shell->ltog);
615:   shell->ltog = ltog;
616:   return(0);
617: }

619: /*@
620:    DMShellSetLocalToLocalVecScatter - Sets a VecScatter context for local to local communication

622:    Logically Collective on dm

624:    Input Arguments
625: +  dm - the shell DM
626: -  ltol - the local to local VecScatter context

628:    Level: advanced

630: .seealso: DMShellSetLocalToLocal(), DMLocalToLocalBeginDefaultShell(), DMLocalToLocalEndDefaultShell()
631: @*/
632: PetscErrorCode DMShellSetLocalToLocalVecScatter(DM dm, VecScatter ltol)
633: {
634:   DM_Shell       *shell = (DM_Shell*)dm->data;

640:   PetscObjectReference((PetscObject)ltol);
641:   VecScatterDestroy(&shell->ltol);
642:   shell->ltol = ltol;
643:   return(0);
644: }

646: /*@C
647:    DMShellSetCoarsen - Set the routine used to coarsen the shell DM

649:    Logically Collective on dm

651:    Input Arguments
652: +  dm - the shell DM
653: -  coarsen - the routine that coarsens the DM

655:    Level: advanced

657: .seealso: DMShellSetRefine(), DMCoarsen(), DMShellGetCoarsen(), DMShellSetContext(), DMShellGetContext()
658: @*/
659: PetscErrorCode DMShellSetCoarsen(DM dm, PetscErrorCode (*coarsen)(DM,MPI_Comm,DM*))
660: {
662:   PetscBool      isshell;

666:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
667:   if (!isshell) return(0);
668:   dm->ops->coarsen = coarsen;
669:   return(0);
670: }

672: /*@C
673:    DMShellGetCoarsen - Get the routine used to coarsen the shell DM

675:    Logically Collective on dm

677:    Input Argument:
678: .  dm - the shell DM

680:    Output Argument:
681: .  coarsen - the routine that coarsens the DM

683:    Level: advanced

685: .seealso: DMShellSetCoarsen(), DMCoarsen(), DMShellSetRefine(), DMRefine()
686: @*/
687: PetscErrorCode DMShellGetCoarsen(DM dm, PetscErrorCode (**coarsen)(DM,MPI_Comm,DM*))
688: {
690:   PetscBool      isshell;

694:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
695:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
696:   *coarsen = dm->ops->coarsen;
697:   return(0);
698: }

700: /*@C
701:    DMShellSetRefine - Set the routine used to refine the shell DM

703:    Logically Collective on dm

705:    Input Arguments
706: +  dm - the shell DM
707: -  refine - the routine that refines the DM

709:    Level: advanced

711: .seealso: DMShellSetCoarsen(), DMRefine(), DMShellGetRefine(), DMShellSetContext(), DMShellGetContext()
712: @*/
713: PetscErrorCode DMShellSetRefine(DM dm, PetscErrorCode (*refine)(DM,MPI_Comm,DM*))
714: {
716:   PetscBool      isshell;

720:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
721:   if (!isshell) return(0);
722:   dm->ops->refine = refine;
723:   return(0);
724: }

726: /*@C
727:    DMShellGetRefine - Get the routine used to refine the shell DM

729:    Logically Collective on dm

731:    Input Argument:
732: .  dm - the shell DM

734:    Output Argument:
735: .  refine - the routine that refines the DM

737:    Level: advanced

739: .seealso: DMShellSetCoarsen(), DMCoarsen(), DMShellSetRefine(), DMRefine()
740: @*/
741: PetscErrorCode DMShellGetRefine(DM dm, PetscErrorCode (**refine)(DM,MPI_Comm,DM*))
742: {
744:   PetscBool      isshell;

748:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
749:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
750:   *refine = dm->ops->refine;
751:   return(0);
752: }

754: /*@C
755:    DMShellSetCreateInterpolation - Set the routine used to create the interpolation operator

757:    Logically Collective on dm

759:    Input Arguments
760: +  dm - the shell DM
761: -  interp - the routine to create the interpolation

763:    Level: advanced

765: .seealso: DMShellSetCreateInjection(), DMCreateInterpolation(), DMShellGetCreateInterpolation(), DMShellSetCreateRestriction(), DMShellSetContext(), DMShellGetContext()
766: @*/
767: PetscErrorCode DMShellSetCreateInterpolation(DM dm, PetscErrorCode (*interp)(DM,DM,Mat*,Vec*))
768: {
770:   PetscBool      isshell;

774:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
775:   if (!isshell) return(0);
776:   dm->ops->createinterpolation = interp;
777:   return(0);
778: }

780: /*@C
781:    DMShellGetCreateInterpolation - Get the routine used to create the interpolation operator

783:    Logically Collective on dm

785:    Input Argument:
786: +  dm - the shell DM

788:    Output Argument:
789: -  interp - the routine to create the interpolation

791:    Level: advanced

793: .seealso: DMShellGetCreateInjection(), DMCreateInterpolation(), DMShellGetCreateRestriction(), DMShellSetContext(), DMShellGetContext()
794: @*/
795: PetscErrorCode DMShellGetCreateInterpolation(DM dm, PetscErrorCode (**interp)(DM,DM,Mat*,Vec*))
796: {
798:   PetscBool      isshell;

802:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
803:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
804:   *interp = dm->ops->createinterpolation;
805:   return(0);
806: }

808: /*@C
809:    DMShellSetCreateRestriction - Set the routine used to create the restriction operator

811:    Logically Collective on dm

813:    Input Arguments
814: +  dm - the shell DM
815: -  striction- the routine to create the restriction

817:    Level: advanced

819: .seealso: DMShellSetCreateInjection(), DMCreateInterpolation(), DMShellGetCreateRestriction(), DMShellSetContext(), DMShellGetContext()
820: @*/
821: PetscErrorCode DMShellSetCreateRestriction(DM dm, PetscErrorCode (*restriction)(DM,DM,Mat*))
822: {
824:   PetscBool      isshell;

828:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
829:   if (!isshell) return(0);
830:   dm->ops->createrestriction = restriction;
831:   return(0);
832: }

834: /*@C
835:    DMShellGetCreateRestriction - Get the routine used to create the restriction operator

837:    Logically Collective on dm

839:    Input Argument:
840: +  dm - the shell DM

842:    Output Argument:
843: -  restriction - the routine to create the restriction

845:    Level: advanced

847: .seealso: DMShellSetCreateInjection(), DMCreateInterpolation(), DMShellSetContext(), DMShellGetContext()
848: @*/
849: PetscErrorCode DMShellGetCreateRestriction(DM dm, PetscErrorCode (**restriction)(DM,DM,Mat*))
850: {
852:   PetscBool      isshell;

856:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
857:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
858:   *restriction = dm->ops->createrestriction;
859:   return(0);
860: }

862: /*@C
863:    DMShellSetCreateInjection - Set the routine used to create the injection operator

865:    Logically Collective on dm

867:    Input Arguments
868: +  dm - the shell DM
869: -  inject - the routine to create the injection

871:    Level: advanced

873: .seealso: DMShellSetCreateInterpolation(), DMCreateInjection(), DMShellGetCreateInjection(), DMShellSetContext(), DMShellGetContext()
874: @*/
875: PetscErrorCode DMShellSetCreateInjection(DM dm, PetscErrorCode (*inject)(DM,DM,Mat*))
876: {
878:   PetscBool      isshell;

882:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
883:   if (!isshell) return(0);
884:   dm->ops->createinjection = inject;
885:   return(0);
886: }

888: /*@C
889:    DMShellGetCreateInjection - Get the routine used to create the injection operator

891:    Logically Collective on dm

893:    Input Argument:
894: +  dm - the shell DM

896:    Output Argument:
897: -  inject - the routine to create the injection

899:    Level: advanced

901: .seealso: DMShellGetCreateInterpolation(), DMCreateInjection(), DMShellSetContext(), DMShellGetContext()
902: @*/
903: PetscErrorCode DMShellGetCreateInjection(DM dm, PetscErrorCode (**inject)(DM,DM,Mat*))
904: {
906:   PetscBool      isshell;

910:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
911:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
912:   *inject = dm->ops->createinjection;
913:   return(0);
914: }

916: /*@C
917:    DMShellSetCreateFieldDecomposition - Set the routine used to create a decomposition of fields for the shell DM

919:    Logically Collective on dm

921:    Input Arguments
922: +  dm - the shell DM
923: -  decomp - the routine to create the decomposition

925:    Level: advanced

927: .seealso: DMCreateFieldDecomposition(), DMShellSetContext(), DMShellGetContext()
928: @*/
929: PetscErrorCode DMShellSetCreateFieldDecomposition(DM dm, PetscErrorCode (*decomp)(DM,PetscInt*,char***, IS**,DM**))
930: {
932:   PetscBool      isshell;

936:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
937:   if (!isshell) return(0);
938:   dm->ops->createfielddecomposition = decomp;
939:   return(0);
940: }

942: /*@C
943:    DMShellSetCreateDomainDecomposition - Set the routine used to create a domain decomposition for the shell DM

945:    Logically Collective on dm

947:    Input Arguments
948: +  dm - the shell DM
949: -  decomp - the routine to create the decomposition

951:    Level: advanced

953: .seealso: DMCreateDomainDecomposition(), DMShellSetContext(), DMShellGetContext()
954: @*/
955: PetscErrorCode DMShellSetCreateDomainDecomposition(DM dm, PetscErrorCode (*decomp)(DM,PetscInt*,char***, IS**,IS**,DM**))
956: {
958:   PetscBool      isshell;

962:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
963:   if (!isshell) return(0);
964:   dm->ops->createdomaindecomposition = decomp;
965:   return(0);
966: }

968: /*@C
969:    DMShellSetCreateDomainDecompositionScatters - Set the routine used to create the scatter contexts for domain decomposition with a shell DM

971:    Logically Collective on dm

973:    Input Arguments
974: +  dm - the shell DM
975: -  scatter - the routine to create the scatters

977:    Level: advanced

979: .seealso: DMCreateDomainDecompositionScatters(), DMShellSetContext(), DMShellGetContext()
980: @*/
981: PetscErrorCode DMShellSetCreateDomainDecompositionScatters(DM dm, PetscErrorCode (*scatter)(DM,PetscInt,DM*,VecScatter**,VecScatter**,VecScatter**))
982: {
984:   PetscBool      isshell;

988:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
989:   if (!isshell) return(0);
990:   dm->ops->createddscatters = scatter;
991:   return(0);
992: }

994: /*@C
995:    DMShellSetCreateSubDM - Set the routine used to create a sub DM from the shell DM

997:    Logically Collective on dm

999:    Input Arguments
1000: +  dm - the shell DM
1001: -  subdm - the routine to create the decomposition

1003:    Level: advanced

1005: .seealso: DMCreateSubDM(), DMShellGetCreateSubDM(), DMShellSetContext(), DMShellGetContext()
1006: @*/
1007: PetscErrorCode DMShellSetCreateSubDM(DM dm, PetscErrorCode (*subdm)(DM,PetscInt,const PetscInt[],IS*,DM*))
1008: {
1010:   PetscBool      isshell;

1014:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
1015:   if (!isshell) return(0);
1016:   dm->ops->createsubdm = subdm;
1017:   return(0);
1018: }

1020: /*@C
1021:    DMShellGetCreateSubDM - Get the routine used to create a sub DM from the shell DM

1023:    Logically Collective on dm

1025:    Input Argument:
1026: +  dm - the shell DM

1028:    Output Argument:
1029: -  subdm - the routine to create the decomposition

1031:    Level: advanced

1033: .seealso: DMCreateSubDM(), DMShellSetCreateSubDM(), DMShellSetContext(), DMShellGetContext()
1034: @*/
1035: PetscErrorCode DMShellGetCreateSubDM(DM dm, PetscErrorCode (**subdm)(DM,PetscInt,const PetscInt[],IS*,DM*))
1036: {
1038:   PetscBool      isshell;

1042:   PetscObjectTypeCompare((PetscObject)dm,DMSHELL,&isshell);
1043:   if (!isshell) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Can only use with DMSHELL type DMs");
1044:   *subdm = dm->ops->createsubdm;
1045:   return(0);
1046: }

1048: static PetscErrorCode DMDestroy_Shell(DM dm)
1049: {
1051:   DM_Shell       *shell = (DM_Shell*)dm->data;

1054:   MatDestroy(&shell->A);
1055:   VecDestroy(&shell->Xglobal);
1056:   VecDestroy(&shell->Xlocal);
1057:   VecScatterDestroy(&shell->gtol);
1058:   VecScatterDestroy(&shell->ltog);
1059:   VecScatterDestroy(&shell->ltol);
1060:   /* This was originally freed in DMDestroy(), but that prevents reference counting of backend objects */
1061:   PetscFree(shell);
1062:   return(0);
1063: }

1065: static PetscErrorCode DMView_Shell(DM dm,PetscViewer v)
1066: {
1068:   DM_Shell       *shell = (DM_Shell*)dm->data;

1071:   VecView(shell->Xglobal,v);
1072:   return(0);
1073: }

1075: static PetscErrorCode DMLoad_Shell(DM dm,PetscViewer v)
1076: {
1078:   DM_Shell       *shell = (DM_Shell*)dm->data;

1081:   VecCreate(PetscObjectComm((PetscObject)dm),&shell->Xglobal);
1082:   VecLoad(shell->Xglobal,v);
1083:   return(0);
1084: }

1086: PetscErrorCode DMCreateSubDM_Shell(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1087: {

1091:   if (subdm) {DMShellCreate(PetscObjectComm((PetscObject) dm), subdm);}
1092:   DMCreateSectionSubDM(dm, numFields, fields, is, subdm);
1093:   return(0);
1094: }

1096: PETSC_EXTERN PetscErrorCode DMCreate_Shell(DM dm)
1097: {
1099:   DM_Shell       *shell;

1102:   PetscNewLog(dm,&shell);
1103:   dm->data = shell;

1105:   dm->ops->destroy            = DMDestroy_Shell;
1106:   dm->ops->createglobalvector = DMCreateGlobalVector_Shell;
1107:   dm->ops->createlocalvector  = DMCreateLocalVector_Shell;
1108:   dm->ops->creatematrix       = DMCreateMatrix_Shell;
1109:   dm->ops->view               = DMView_Shell;
1110:   dm->ops->load               = DMLoad_Shell;
1111:   dm->ops->globaltolocalbegin = DMGlobalToLocalBeginDefaultShell;
1112:   dm->ops->globaltolocalend   = DMGlobalToLocalEndDefaultShell;
1113:   dm->ops->localtoglobalbegin = DMLocalToGlobalBeginDefaultShell;
1114:   dm->ops->localtoglobalend   = DMLocalToGlobalEndDefaultShell;
1115:   dm->ops->localtolocalbegin  = DMLocalToLocalBeginDefaultShell;
1116:   dm->ops->localtolocalend    = DMLocalToLocalEndDefaultShell;
1117:   dm->ops->createsubdm        = DMCreateSubDM_Shell;
1118:   DMSetMatType(dm,MATDENSE);
1119:   return(0);
1120: }

1122: /*@
1123:     DMShellCreate - Creates a shell DM object, used to manage user-defined problem data

1125:     Collective

1127:     Input Parameter:
1128: .   comm - the processors that will share the global vector

1130:     Output Parameters:
1131: .   shell - the shell DM

1133:     Level: advanced

1135: .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateLocalVector(), DMShellSetContext(), DMShellGetContext()
1136: @*/
1137: PetscErrorCode  DMShellCreate(MPI_Comm comm,DM *dm)
1138: {

1143:   DMCreate(comm,dm);
1144:   DMSetType(*dm,DMSHELL);
1145:   DMSetUp(*dm);
1146:   return(0);
1147: }