Actual source code: inherit.c

  1: /*
  2:      Provides utility routines for manipulating any type of PETSc object.
  3: */
  4: #include <petsc/private/petscimpl.h>
  5: #include <petscviewer.h>

  7: PETSC_INTERN PetscObject *PetscObjects;
  8: PETSC_INTERN PetscInt     PetscObjectsCounts;
  9: PETSC_INTERN PetscInt     PetscObjectsMaxCounts;
 10: PETSC_INTERN PetscBool    PetscObjectsLog;

 12: PetscObject *PetscObjects       = NULL;
 13: PetscInt     PetscObjectsCounts = 0, PetscObjectsMaxCounts = 0;
 14: PetscBool    PetscObjectsLog = PETSC_FALSE;

 16: PetscObjectId PetscObjectNewId_Internal(void)
 17: {
 18:   static PetscObjectId idcnt = 1;
 19:   return idcnt++;
 20: }

 22: PetscErrorCode PetscHeaderCreate_Function(PetscErrorCode ierr, PetscObject *h, PetscClassId classid, const char class_name[], const char descr[], const char mansec[], MPI_Comm comm, PetscObjectDestroyFn *destroy, PetscObjectViewFn *view)
 23: {
 24:   PetscFunctionBegin;
 25:   if (ierr) PetscFunctionReturn(ierr);
 26:   PetscCall(PetscHeaderCreate_Private(*h, classid, class_name, descr, mansec, comm, destroy, view));
 27:   PetscCall(PetscLogObjectCreate(*h));
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }

 31: /*
 32:    PetscHeaderCreate_Private - Fills in the default values.
 33: */
 34: PetscErrorCode PetscHeaderCreate_Private(PetscObject h, PetscClassId classid, const char class_name[], const char descr[], const char mansec[], MPI_Comm comm, PetscObjectDestroyFn *destroy, PetscObjectViewFn *view)
 35: {
 36:   void       *get_tmp;
 37:   PetscInt64 *cidx;
 38:   PetscMPIInt iflg;

 40:   PetscFunctionBegin;
 41:   h->classid               = classid;
 42:   h->class_name            = (char *)class_name;
 43:   h->description           = (char *)descr;
 44:   h->mansec                = (char *)mansec;
 45:   h->refct                 = 1;
 46:   h->non_cyclic_references = NULL;
 47:   h->id                    = PetscObjectNewId_Internal();
 48:   h->bops->destroy         = destroy;
 49:   h->bops->view            = view;

 51:   PetscCall(PetscCommDuplicate(comm, &h->comm, &h->tag));

 53:   /* Increment and store current object creation index */
 54:   PetscCallMPI(MPI_Comm_get_attr(h->comm, Petsc_CreationIdx_keyval, &get_tmp, &iflg));
 55:   PetscCheck(iflg, h->comm, PETSC_ERR_ARG_CORRUPT, "MPI_Comm does not have an object creation index");
 56:   cidx    = (PetscInt64 *)get_tmp;
 57:   h->cidx = (*cidx)++;

 59:   /* Keep a record of object created */
 60:   if (PetscDefined(USE_LOG) && PetscObjectsLog) {
 61:     PetscObject *newPetscObjects;
 62:     PetscInt     newPetscObjectsMaxCounts;

 64:     PetscObjectsCounts++;
 65:     for (PetscInt i = 0; i < PetscObjectsMaxCounts; ++i) {
 66:       if (!PetscObjects[i]) {
 67:         PetscObjects[i] = h;
 68:         PetscFunctionReturn(PETSC_SUCCESS);
 69:       }
 70:     }
 71:     /* Need to increase the space for storing PETSc objects */
 72:     if (!PetscObjectsMaxCounts) newPetscObjectsMaxCounts = 100;
 73:     else newPetscObjectsMaxCounts = 2 * PetscObjectsMaxCounts;
 74:     PetscCall(PetscCalloc1(newPetscObjectsMaxCounts, &newPetscObjects));
 75:     PetscCall(PetscArraycpy(newPetscObjects, PetscObjects, PetscObjectsMaxCounts));
 76:     PetscCall(PetscFree(PetscObjects));

 78:     PetscObjects                        = newPetscObjects;
 79:     PetscObjects[PetscObjectsMaxCounts] = h;
 80:     PetscObjectsMaxCounts               = newPetscObjectsMaxCounts;
 81:   }
 82:   PetscFunctionReturn(PETSC_SUCCESS);
 83: }

 85: PETSC_INTERN PetscBool      PetscMemoryCollectMaximumUsage;
 86: PETSC_INTERN PetscLogDouble PetscMemoryMaximumUsage;

 88: PetscErrorCode PetscHeaderDestroy_Function(PetscObject *h)
 89: {
 90:   PetscFunctionBegin;
 91:   PetscCall(PetscLogObjectDestroy(*h));
 92:   PetscCall(PetscHeaderDestroy_Private(*h, PETSC_FALSE));
 93:   PetscCall(PetscFree(*h));
 94:   PetscFunctionReturn(PETSC_SUCCESS);
 95: }

 97: /*
 98:     PetscHeaderDestroy_Private - Destroys a base PETSc object header. Called by
 99:     the macro PetscHeaderDestroy().
100: */
101: PetscErrorCode PetscHeaderDestroy_Private(PetscObject obj, PetscBool clear_for_reuse)
102: {
103:   PetscFunctionBegin;
105:   PetscCall(PetscComposedQuantitiesDestroy(obj));
106:   if (PetscMemoryCollectMaximumUsage) {
107:     PetscLogDouble usage;

109:     PetscCall(PetscMemoryGetCurrentUsage(&usage));
110:     if (usage > PetscMemoryMaximumUsage) PetscMemoryMaximumUsage = usage;
111:   }
112:   /* first destroy things that could execute arbitrary code */
113:   if (obj->python_destroy) {
114:     void *python_context                     = obj->python_context;
115:     PetscErrorCode (*python_destroy)(void *) = obj->python_destroy;

117:     obj->python_context = NULL;
118:     obj->python_destroy = NULL;
119:     PetscCall((*python_destroy)(python_context));
120:   }
121:   PetscCall(PetscObjectDestroyOptionsHandlers(obj));
122:   PetscCall(PetscObjectListDestroy(&obj->olist));

124:   /* destroy allocated quantities */
125:   if (PetscPrintFunctionList) PetscCall(PetscFunctionListPrintNonEmpty(obj->qlist));
126:   PetscCheck(--obj->refct <= 0, obj->comm, PETSC_ERR_PLIB, "Destroying a PetscObject (%s) with reference count %" PetscInt_FMT " >= 1", obj->name ? obj->name : "unnamed", obj->refct);
127:   PetscCall(PetscFree(obj->name));
128:   PetscCall(PetscFree(obj->prefix));
129:   PetscCall(PetscFree(obj->type_name));

131:   if (clear_for_reuse) {
132:     /* we will assume that obj->bops->view and destroy are safe to leave as-is */

134:     /* reset quantities, in order of appearance in _p_PetscObject */
135:     obj->id       = PetscObjectNewId_Internal();
136:     obj->refct    = 1;
137:     obj->tablevel = 0;
138:     obj->state    = 0;
139:     /* don't deallocate, zero these out instead */
140:     PetscCall(PetscFunctionListClear(obj->qlist));
141:     PetscCall(PetscArrayzero(obj->fortran_func_pointers, obj->num_fortran_func_pointers));
142:     PetscCall(PetscArrayzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_CLASS], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_CLASS]));
143:     PetscCall(PetscArrayzero(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE], obj->num_fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]));
144:     obj->optionsprinted = PETSC_FALSE;
145: #if PetscDefined(HAVE_SAWS)
146:     obj->amsmem          = PETSC_FALSE;
147:     obj->amspublishblock = PETSC_FALSE;
148: #endif
149:     obj->options                                  = NULL;
150:     obj->donotPetscObjectPrintClassNamePrefixType = PETSC_FALSE;
151:   } else {
152:     PetscCall(PetscFunctionListDestroy(&obj->qlist));
153:     PetscCall(PetscFree(obj->fortran_func_pointers));
154:     PetscCall(PetscFree(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_CLASS]));
155:     PetscCall(PetscFree(obj->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]));
156:     PetscCall(PetscCommDestroy(&obj->comm));
157:     obj->classid = PETSCFREEDHEADER;

159:     if (PetscDefined(USE_LOG) && PetscObjectsLog) {
160:       /* Record object removal from list of all objects */
161:       for (PetscInt i = 0; i < PetscObjectsMaxCounts; ++i) {
162:         if (PetscObjects[i] == obj) {
163:           PetscObjects[i] = NULL;
164:           --PetscObjectsCounts;
165:           break;
166:         }
167:       }
168:       if (!PetscObjectsCounts) {
169:         PetscCall(PetscFree(PetscObjects));
170:         PetscObjectsMaxCounts = 0;
171:       }
172:     }
173:   }
174:   PetscFunctionReturn(PETSC_SUCCESS);
175: }

177: /*
178:   PetscHeaderReset_Internal - "Reset" a PetscObject header. This is tantamount to destroying
179:   the object but does not free all resources. The object retains its:

181:   - classid
182:   - bops->view
183:   - bops->destroy
184:   - comm
185:   - tag
186:   - class_name
187:   - description
188:   - mansec
189:   - cpp

191:   Note that while subclass information is lost, superclass info remains. Thus this function is
192:   intended to be used to reuse a PetscObject within the same class to avoid reallocating its
193:   resources.
194: */
195: PetscErrorCode PetscHeaderReset_Internal(PetscObject obj)
196: {
197:   PetscFunctionBegin;
198:   PetscCall(PetscHeaderDestroy_Private(obj, PETSC_TRUE));
199:   PetscFunctionReturn(PETSC_SUCCESS);
200: }

202: /*@
203:   PetscObjectCopyFortranFunctionPointers - Copy function pointers to another object

205:   Logically Collective

207:   Input Parameters:
208: + src  - source object
209: - dest - destination object

211:   Level: developer

213:   Note:
214:   Both objects must have the same class.

216:   This is used to help manage user callback functions that were provided in Fortran

218: .seealso: `PetscFortranCallbackRegister()`, `PetscFortranCallbackGetSizes()`
219: @*/
220: PetscErrorCode PetscObjectCopyFortranFunctionPointers(PetscObject src, PetscObject dest)
221: {
222:   PetscFortranCallbackId cbtype;

224:   PetscFunctionBegin;
227:   PetscCheck(src->classid == dest->classid, src->comm, PETSC_ERR_ARG_INCOMP, "Objects must be of the same class");

229:   PetscCall(PetscFree(dest->fortran_func_pointers));
230:   PetscCall(PetscMalloc(src->num_fortran_func_pointers * sizeof(PetscFortranCallbackFn *), &dest->fortran_func_pointers));
231:   PetscCall(PetscArraycpy(dest->fortran_func_pointers, src->fortran_func_pointers, src->num_fortran_func_pointers));

233:   dest->num_fortran_func_pointers = src->num_fortran_func_pointers;

235:   for (cbtype = PETSC_FORTRAN_CALLBACK_CLASS; cbtype < PETSC_FORTRAN_CALLBACK_MAXTYPE; cbtype++) {
236:     PetscCall(PetscFree(dest->fortrancallback[cbtype]));
237:     PetscCall(PetscCalloc1(src->num_fortrancallback[cbtype], &dest->fortrancallback[cbtype]));
238:     PetscCall(PetscArraycpy(dest->fortrancallback[cbtype], src->fortrancallback[cbtype], src->num_fortrancallback[cbtype]));
239:     dest->num_fortrancallback[cbtype] = src->num_fortrancallback[cbtype];
240:   }
241:   PetscFunctionReturn(PETSC_SUCCESS);
242: }

244: /*@C
245:   PetscObjectSetFortranCallback - set Fortran callback function pointer and context

247:   Logically Collective, No Fortran Support

249:   Input Parameters:
250: + obj    - object on which to set callback
251: . cbtype - callback type (class or subtype)
252: . cid    - address of callback Id, updated if not yet initialized (zero)
253: . func   - Fortran function
254: - ctx    - Fortran context

256:   Level: developer

258:   Note:
259:   This is used to help manage user callback functions that were provided in Fortran

261: .seealso: `PetscObjectGetFortranCallback()`, `PetscFortranCallbackRegister()`, `PetscFortranCallbackGetSizes()`
262: @*/
263: PetscErrorCode PetscObjectSetFortranCallback(PetscObject obj, PetscFortranCallbackType cbtype, PetscFortranCallbackId *cid, PetscFortranCallbackFn *func, PetscCtx ctx)
264: {
265:   const char *subtype = NULL;

267:   PetscFunctionBegin;
269:   if (cbtype == PETSC_FORTRAN_CALLBACK_SUBTYPE) subtype = obj->type_name;
270:   if (!*cid) PetscCall(PetscFortranCallbackRegister(obj->classid, subtype, cid));
271:   if (*cid >= PETSC_SMALLEST_FORTRAN_CALLBACK + obj->num_fortrancallback[cbtype]) {
272:     PetscFortranCallbackId oldnum = obj->num_fortrancallback[cbtype];
273:     PetscFortranCallbackId newnum = PetscMax(*cid - PETSC_SMALLEST_FORTRAN_CALLBACK + 1, 2 * oldnum);
274:     PetscFortranCallback  *callback;
275:     PetscCall(PetscMalloc1(newnum, &callback));
276:     PetscCall(PetscArraycpy(callback, obj->fortrancallback[cbtype], oldnum));
277:     PetscCall(PetscFree(obj->fortrancallback[cbtype]));

279:     obj->fortrancallback[cbtype]     = callback;
280:     obj->num_fortrancallback[cbtype] = newnum;
281:   }
282:   obj->fortrancallback[cbtype][*cid - PETSC_SMALLEST_FORTRAN_CALLBACK].func = func;
283:   obj->fortrancallback[cbtype][*cid - PETSC_SMALLEST_FORTRAN_CALLBACK].ctx  = ctx;
284:   PetscFunctionReturn(PETSC_SUCCESS);
285: }

287: /*@C
288:   PetscObjectGetFortranCallback - get Fortran callback function pointer and context

290:   Logically Collective, No Fortran Support

292:   Input Parameters:
293: + obj    - object on which to get callback
294: . cbtype - callback type
295: - cid    - address of callback Id

297:   Output Parameters:
298: + func - Fortran function (or `NULL` if not needed)
299: - ctx  - Fortran context (or `NULL` if not needed)

301:   Level: developer

303:   Note:
304:   This is used to help manage user callback functions that were provided in Fortran

306: .seealso: `PetscObjectSetFortranCallback()`, `PetscFortranCallbackRegister()`, `PetscFortranCallbackGetSizes()`
307: @*/
308: PetscErrorCode PetscObjectGetFortranCallback(PetscObject obj, PetscFortranCallbackType cbtype, PetscFortranCallbackId cid, PetscFortranCallbackFn **func, void **ctx)
309: {
310:   PetscFortranCallback *cb;

312:   PetscFunctionBegin;
314:   PetscCheck(cid >= PETSC_SMALLEST_FORTRAN_CALLBACK, obj->comm, PETSC_ERR_ARG_CORRUPT, "Fortran callback Id invalid");
315:   PetscCheck(cid < PETSC_SMALLEST_FORTRAN_CALLBACK + obj->num_fortrancallback[cbtype], obj->comm, PETSC_ERR_ARG_CORRUPT, "Fortran callback not set on this object");
316:   cb = &obj->fortrancallback[cbtype][cid - PETSC_SMALLEST_FORTRAN_CALLBACK];
317:   if (func) *func = cb->func;
318:   if (ctx) *ctx = cb->ctx;
319:   PetscFunctionReturn(PETSC_SUCCESS);
320: }

322: #if defined(PETSC_USE_LOG)
323: /*@C
324:   PetscObjectsDump - Prints all the currently existing objects.

326:   Input Parameters:
327: + fd  - file pointer
328: - all - by default only tries to display objects created explicitly by the user, if all is `PETSC_TRUE` then lists all outstanding objects

330:   Options Database Key:
331: . -objects_dump all - print information about all the objects that exist at the end of the programs run

333:   Level: advanced

335:   Note:
336:   Only MPI rank 0 of `PETSC_COMM_WORLD` prints the values

338: .seealso: `PetscObject`
339: @*/
340: PetscErrorCode PetscObjectsDump(FILE *fd, PetscBool all)
341: {
342:   PetscInt    i, j, k = 0;
343:   PetscObject h;

345:   PetscFunctionBegin;
346:   if (PetscObjectsCounts) {
347:     PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "The following objects were never freed\n"));
348:     PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "-----------------------------------------\n"));
349:     for (i = 0; i < PetscObjectsMaxCounts; i++) {
350:       if ((h = PetscObjects[i])) {
351:         PetscCall(PetscObjectName(h));
352:         {
353:           PetscStack *stack  = NULL;
354:           char       *create = NULL, *rclass = NULL;

356:           /* if the PETSc function the user calls is not a create then this object was NOT directly created by them */
357:           PetscCall(PetscMallocGetStack(h, &stack));
358:           if (stack) {
359:             k = stack->currentsize - 2;
360:             if (!all) {
361:               k = 0;
362:               while (!stack->petscroutine[k]) k++;
363:               PetscCall(PetscStrstr(stack->function[k], "Create", &create));
364:               if (!create) PetscCall(PetscStrstr(stack->function[k], "Get", &create));
365:               PetscCall(PetscStrstr(stack->function[k], h->class_name, &rclass));
366:               if (!create) continue;
367:               if (!rclass) continue;
368:             }
369:           }

371:           PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "[%d] %s %s %s\n", PetscGlobalRank, h->class_name, h->type_name, h->name));

373:           PetscCall(PetscMallocGetStack(h, &stack));
374:           if (stack) {
375:             for (j = k; j >= 0; j--) fprintf(fd, "      [%d]  %s() in %s\n", PetscGlobalRank, stack->function[j], stack->file[j]);
376:           }
377:         }
378:       }
379:     }
380:   }
381:   PetscFunctionReturn(PETSC_SUCCESS);
382: }

384: /*@
385:   PetscObjectsView - Prints the currently existing objects.

387:   Logically Collective

389:   Input Parameter:
390: . viewer - must be an `PETSCVIEWERASCII` viewer

392:   Level: advanced

394: .seealso: `PetscObject`
395: @*/
396: PetscErrorCode PetscObjectsView(PetscViewer viewer)
397: {
398:   PetscBool isascii;
399:   FILE     *fd;

401:   PetscFunctionBegin;
402:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
403:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
404:   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Only supports ASCII viewer");
405:   PetscCall(PetscViewerASCIIGetPointer(viewer, &fd));
406:   PetscCall(PetscObjectsDump(fd, PETSC_TRUE));
407:   PetscFunctionReturn(PETSC_SUCCESS);
408: }

410: /*@
411:   PetscObjectsGetObject - Get a pointer to a named object

413:   Not Collective

415:   Input Parameter:
416: . name - the name of an object

418:   Output Parameters:
419: + obj       - the object or `NULL` if there is no object, optional, pass in `NULL` if not needed
420: - classname - the name of the class of the object, optional, pass in `NULL` if not needed

422:   Level: advanced

424: .seealso: `PetscObject`
425: @*/
426: PetscErrorCode PetscObjectsGetObject(const char name[], PetscObject *obj, const char *classname[])
427: {
428:   PetscInt    i;
429:   PetscObject h;
430:   PetscBool   flg;

432:   PetscFunctionBegin;
433:   PetscAssertPointer(name, 1);
434:   if (obj) *obj = NULL;
435:   for (i = 0; i < PetscObjectsMaxCounts; i++) {
436:     if ((h = PetscObjects[i])) {
437:       PetscCall(PetscObjectName(h));
438:       PetscCall(PetscStrcmp(h->name, name, &flg));
439:       if (flg) {
440:         if (obj) *obj = h;
441:         if (classname) *classname = h->class_name;
442:         PetscFunctionReturn(PETSC_SUCCESS);
443:       }
444:     }
445:   }
446:   PetscFunctionReturn(PETSC_SUCCESS);
447: }
448: #else
449: PetscErrorCode PetscObjectsView(PetscViewer viewer)
450: {
451:   PetscFunctionReturn(PETSC_SUCCESS);
452: }

454: PetscErrorCode PetscObjectsGetObject(const char name[], PetscObject *obj, const char *classname[])
455: {
456:   PetscFunctionReturn(PETSC_SUCCESS);
457: }
458: #endif

460: /*@
461:   PetscObjectSetPrintedOptions - indicate to an object that it should behave as if it has already printed the help for its options so it will not display the help message

463:   Input Parameter:
464: . obj - the `PetscObject`

466:   Level: developer

468:   Developer Notes:
469:   This is used, for example to prevent sequential objects that are created from a parallel object; such as the `KSP` created by
470:   `PCBJACOBI` from all printing the same help messages to the screen

472: .seealso: `PetscOptionsInsert()`, `PetscObject`
473: @*/
474: PetscErrorCode PetscObjectSetPrintedOptions(PetscObject obj)
475: {
476:   PetscFunctionBegin;
477:   PetscAssertPointer(obj, 1);
478:   obj->optionsprinted = PETSC_TRUE;
479:   PetscFunctionReturn(PETSC_SUCCESS);
480: }

482: /*@
483:   PetscObjectInheritPrintedOptions - If the child object is not on the MPI rank 0 process of the parent object and the child is sequential then the child gets it set.

485:   Input Parameters:
486: + pobj - the parent object
487: - obj  - the `PetscObject`

489:   Level: developer

491:   Developer Notes:
492:   This is used, for example to prevent sequential objects that are created from a parallel object; such as the `KSP` created by
493:   `PCBJACOBI` from all printing the same help messages to the screen

495:   This will not handle more complicated situations like with `PCGASM` where children may live on any subset of the parent's processes and overlap

497: .seealso: `PetscOptionsInsert()`, `PetscObjectSetPrintedOptions()`, `PetscObject`
498: @*/
499: PetscErrorCode PetscObjectInheritPrintedOptions(PetscObject pobj, PetscObject obj)
500: {
501:   PetscMPIInt prank, size;

503:   PetscFunctionBegin;
506:   PetscCallMPI(MPI_Comm_rank(pobj->comm, &prank));
507:   PetscCallMPI(MPI_Comm_size(obj->comm, &size));
508:   if (size == 1 && prank > 0) obj->optionsprinted = PETSC_TRUE;
509:   PetscFunctionReturn(PETSC_SUCCESS);
510: }

512: /*@C
513:   PetscObjectAddOptionsHandler - Adds an additional function to check for options when `XXXSetFromOptions()` is called.

515:   Not Collective

517:   Input Parameters:
518: + obj     - the PETSc object
519: . handle  - function that checks for options
520: . destroy - function to destroy `ctx` if provided
521: - ctx     - optional context for check function

523:   Calling sequence of `handle`:
524: + obj                - the PETSc object
525: . PetscOptionsObject - the `PetscOptionItems` object
526: - ctx                - optional context for `handle`

528:   Calling sequence of `destroy`:
529: + obj - the PETSc object
530: - ctx - optional context for `handle`

532:   Level: developer

534: .seealso: `KSPSetFromOptions()`, `PCSetFromOptions()`, `SNESSetFromOptions()`, `PetscObjectProcessOptionsHandlers()`, `PetscObjectDestroyOptionsHandlers()`,
535:           `PetscObject`
536: @*/
537: PetscErrorCode PetscObjectAddOptionsHandler(PetscObject obj, PetscErrorCode (*handle)(PetscObject obj, PetscOptionItems PetscOptionsObject, PetscCtx ctx), PetscErrorCode (*destroy)(PetscObject obj, PetscCtxRt ctx), PetscCtx ctx)
538: {
539:   PetscFunctionBegin;
541:   for (PetscInt i = 0; i < obj->noptionhandler; i++) {
542:     PetscBool identical = (PetscBool)(obj->optionhandler[i] == handle && obj->optiondestroy[i] == destroy && obj->optionctx[i] == ctx);
543:     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
544:   }
545:   PetscCheck(obj->noptionhandler < PETSC_MAX_OPTIONS_HANDLER, obj->comm, PETSC_ERR_ARG_OUTOFRANGE, "Too many options handlers added");
546:   obj->optionhandler[obj->noptionhandler] = handle;
547:   obj->optiondestroy[obj->noptionhandler] = destroy;
548:   obj->optionctx[obj->noptionhandler++]   = ctx;
549:   PetscFunctionReturn(PETSC_SUCCESS);
550: }

552: /*@C
553:   PetscObjectProcessOptionsHandlers - Calls all the options handlers attached to an object

555:   Not Collective

557:   Input Parameters:
558: + obj                - the PETSc object
559: - PetscOptionsObject - the options context

561:   Level: developer

563: .seealso: `KSPSetFromOptions()`, `PCSetFromOptions()`, `SNESSetFromOptions()`, `PetscObjectAddOptionsHandler()`, `PetscObjectDestroyOptionsHandlers()`,
564:           `PetscObject`
565: @*/
566: PetscErrorCode PetscObjectProcessOptionsHandlers(PetscObject obj, PetscOptionItems PetscOptionsObject)
567: {
568:   PetscFunctionBegin;
570:   for (PetscInt i = 0; i < obj->noptionhandler; i++) PetscCall((*obj->optionhandler[i])(obj, PetscOptionsObject, obj->optionctx[i]));
571:   PetscFunctionReturn(PETSC_SUCCESS);
572: }

574: /*@
575:   PetscObjectDestroyOptionsHandlers - Destroys all the option handlers attached to an object

577:   Not Collective

579:   Input Parameter:
580: . obj - the PETSc object

582:   Level: developer

584: .seealso: `KSPSetFromOptions()`, `PCSetFromOptions()`, `SNESSetFromOptions()`, `PetscObjectAddOptionsHandler()`, `PetscObjectProcessOptionsHandlers()`,
585:           `PetscObject`
586: @*/
587: PetscErrorCode PetscObjectDestroyOptionsHandlers(PetscObject obj)
588: {
589:   PetscFunctionBegin;
591:   for (PetscInt i = 0; i < obj->noptionhandler; i++) {
592:     if (obj->optiondestroy[i]) PetscCall((*obj->optiondestroy[i])(obj, obj->optionctx[i]));
593:   }
594:   obj->noptionhandler = 0;
595:   PetscFunctionReturn(PETSC_SUCCESS);
596: }

598: /*@
599:   PetscObjectReference - Indicates to a `PetscObject` that it is being
600:   referenced by another `PetscObject`. This increases the reference
601:   count for that object by one.

603:   Logically Collective

605:   Input Parameter:
606: . obj - the PETSc object. This must be cast with (`PetscObject`), for example, `PetscObjectReference`((`PetscObject`)mat);

608:   Level: advanced

610:   Note:
611:   If `obj` is `NULL` this function returns without doing anything.

613: .seealso: `PetscObjectCompose()`, `PetscObjectDereference()`, `PetscObject`
614: @*/
615: PetscErrorCode PetscObjectReference(PetscObject obj)
616: {
617:   PetscFunctionBegin;
618:   if (!obj) PetscFunctionReturn(PETSC_SUCCESS);
620:   obj->refct++;
621:   PetscFunctionReturn(PETSC_SUCCESS);
622: }

624: /*@
625:   PetscObjectGetReference - Gets the current reference count for a PETSc object.

627:   Not Collective

629:   Input Parameter:
630: . obj - the PETSc object; this must be cast with (`PetscObject`), for example,
631:         `PetscObjectGetReference`((`PetscObject`)mat,&cnt); `obj` cannot be `NULL`

633:   Output Parameter:
634: . cnt - the reference count

636:   Level: advanced

638: .seealso: `PetscObjectCompose()`, `PetscObjectDereference()`, `PetscObjectReference()`, `PetscObject`
639: @*/
640: PetscErrorCode PetscObjectGetReference(PetscObject obj, PetscInt *cnt)
641: {
642:   PetscFunctionBegin;
644:   PetscAssertPointer(cnt, 2);
645:   *cnt = obj->refct;
646:   PetscFunctionReturn(PETSC_SUCCESS);
647: }

649: /*@
650:   PetscObjectDereference - Indicates to any `PetscObject` that it is being
651:   referenced by one less `PetscObject`. This decreases the reference
652:   count for that object by one.

654:   Collective on `obj` if reference reaches 0 otherwise Logically Collective

656:   Input Parameter:
657: . obj - the PETSc object; this must be cast with (`PetscObject`), for example,
658:         `PetscObjectDereference`((`PetscObject`)mat);

660:   Level: advanced

662:   Notes:
663:   `PetscObjectDestroy()` sets the `obj` pointer to `NULL` after the call, this routine does not.

665:   If `obj` is `NULL` this function returns without doing anything.

667: .seealso: `PetscObjectCompose()`, `PetscObjectReference()`, `PetscObjectDestroy()`, `PetscObject`
668: @*/
669: PetscErrorCode PetscObjectDereference(PetscObject obj)
670: {
671:   PetscFunctionBegin;
672:   if (!obj) PetscFunctionReturn(PETSC_SUCCESS);
674:   if (obj->bops->destroy) PetscCall((*obj->bops->destroy)(&obj));
675:   else PetscCheck(--obj->refct, PETSC_COMM_SELF, PETSC_ERR_SUP, "This PETSc object does not have a generic destroy routine");
676:   PetscFunctionReturn(PETSC_SUCCESS);
677: }

679: /*
680:      The following routines are the versions private to the PETSc object
681:      data structures.
682: */
683: PetscErrorCode PetscObjectRemoveReference(PetscObject obj, const char name[])
684: {
685:   PetscFunctionBegin;
687:   PetscCall(PetscObjectListRemoveReference(&obj->olist, name));
688:   PetscFunctionReturn(PETSC_SUCCESS);
689: }

691: /*@
692:   PetscObjectCompose - Associates another PETSc object with a given PETSc object.

694:   Not Collective

696:   Input Parameters:
697: + obj  - the PETSc object; this must be cast with (`PetscObject`), for example,
698:          `PetscObjectCompose`((`PetscObject`)mat,...);
699: . name - name associated with the child object
700: - ptr  - the other PETSc object to associate with the PETSc object; this must also be
701:          cast with (`PetscObject`)

703:   Level: advanced

705:   Notes:
706:   The second objects reference count is automatically increased by one when it is
707:   composed.

709:   Replaces any previous object that had been composed with the same name.

711:   If `ptr` is `NULL` and `name` has previously been composed using an object, then that
712:   entry is removed from `obj`.

714:   `PetscObjectCompose()` can be used with any PETSc object (such as
715:   `Mat`, `Vec`, `KSP`, `SNES`, etc.) or any user-provided object.

717:   `PetscContainerCreate()` or `PetscObjectContainerCompose()` can be used to create an object from a
718:   user-provided pointer that may then be composed with PETSc objects using `PetscObjectCompose()`

720:   Fortran Note:
721:   Use
722: .vb
723:   call PetscObjectCompose(obj, name, PetscObjectCast(ptr), ierr)
724: .ve

726: .seealso: `PetscObjectQuery()`, `PetscContainerCreate()`, `PetscObjectComposeFunction()`, `PetscObjectQueryFunction()`, `PetscContainer`,
727:           `PetscContainerSetPointer()`, `PetscObject`, `PetscObjectContainerCompose()`
728: @*/
729: PetscErrorCode PetscObjectCompose(PetscObject obj, const char name[], PetscObject ptr)
730: {
731:   PetscFunctionBegin;
733:   PetscAssertPointer(name, 2);
735:   PetscCheck(obj != ptr, PetscObjectComm(obj), PETSC_ERR_SUP, "Cannot compose object with itself");
736:   if (ptr) {
737:     const char *tname;
738:     PetscBool   skipreference;

740:     PetscCall(PetscObjectListReverseFind(ptr->olist, obj, &tname, &skipreference));
741:     if (tname) PetscCheck(skipreference, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "An object cannot be composed with an object that was composed with it");
742:   }
743:   PetscCall(PetscObjectListAdd(&obj->olist, name, ptr));
744:   PetscFunctionReturn(PETSC_SUCCESS);
745: }

747: /*@
748:   PetscObjectQuery - Gets a PETSc object associated with a given object that was composed with `PetscObjectCompose()`

750:   Not Collective

752:   Input Parameters:
753: + obj  - the PETSc object. It must be cast with a (`PetscObject`), for example,
754:          `PetscObjectCompose`((`PetscObject`)mat,...);
755: . name - name associated with child object
756: - ptr  - the other PETSc object associated with the PETSc object, this must be
757:          cast with (`PetscObject`*)

759:   Level: advanced

761:   Note:
762:   The reference count of neither object is increased in this call

764:   Fortran Note:
765:   Use
766: .vb
767:   call PetscObjectQuery(PetscObjectCast(obj), name, ptr, ierr)
768: .ve

770: .seealso: `PetscObjectCompose()`, `PetscObjectComposeFunction()`, `PetscObjectQueryFunction()`, `PetscContainer`,
771:           `PetscContainerGetPointer()`, `PetscObject`
772: @*/
773: PetscErrorCode PetscObjectQuery(PetscObject obj, const char name[], PetscObject *ptr)
774: {
775:   PetscFunctionBegin;
777:   PetscAssertPointer(name, 2);
778:   PetscAssertPointer(ptr, 3);
779:   PetscCall(PetscObjectListFind(obj->olist, name, ptr));
780:   PetscFunctionReturn(PETSC_SUCCESS);
781: }

783: /*MC
784:   PetscObjectComposeFunction - Associates a function with a given PETSc object.

786:   Synopsis:
787: #include <petscsys.h>
788:   PetscErrorCode PetscObjectComposeFunction(PetscObject obj, const char name[], PetscErrorCodeFn *fptr)

790:   Logically Collective

792:   Input Parameters:
793: + obj  - the PETSc object; this must be cast with a (`PetscObject`), for example,
794:          `PetscObjectCompose`((`PetscObject`)mat,...);
795: . name - name associated with the child function
796: - fptr - function pointer

798:   Level: advanced

800:   Notes:
801:   When the first argument of `fptr` is (or is derived from) a `PetscObject` then `PetscTryMethod()` and `PetscUseMethod()`
802:   can be used to call the function directly with error checking.

804:   To remove a registered routine, pass in `NULL` for `fptr`.

806:   `PetscObjectComposeFunction()` can be used with any PETSc object (such as
807:   `Mat`, `Vec`, `KSP`, `SNES`, etc.) or any user-provided object.

809:   `PetscUseTypeMethod()` and `PetscTryTypeMethod()` are used to call a function that is stored in the objects `obj->ops` table.

811: .seealso: `PetscObjectQueryFunction()`, `PetscContainerCreate()` `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscTryMethod()`, `PetscUseMethod()`,
812:           `PetscUseTypeMethod()`, `PetscTryTypeMethod()`, `PetscObject`
813: M*/
814: PetscErrorCode PetscObjectComposeFunction_Private(PetscObject obj, const char name[], PetscErrorCodeFn *fptr)
815: {
816:   PetscFunctionBegin;
818:   PetscAssertPointer(name, 2);
819:   PetscCall(PetscFunctionListAdd_Private(&obj->qlist, name, fptr));
820:   PetscFunctionReturn(PETSC_SUCCESS);
821: }

823: PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject obj, const char name[], PetscErrorCodeFn **fptr)
824: {
825:   PetscFunctionBegin;
827:   PetscAssertPointer(name, 2);
828:   PetscCall(PetscFunctionListFind_Private(obj->qlist, name, fptr));
829:   PetscFunctionReturn(PETSC_SUCCESS);
830: }

832: /*@
833:   PetscObjectHasFunction - Query if a function is associated with a given object.

835:   Logically Collective

837:   Input Parameters:
838: + obj  - the PETSc object
839: - name - name associated with the child function

841:   Output Parameter:
842: . has - the boolean value

844:   Level: advanced

846: .seealso: `PetscObject`, `PetscObjectComposeFunction()`, `PetscObjectQueryFunction()`
847: @*/
848: PetscErrorCode PetscObjectHasFunction(PetscObject obj, const char name[], PetscBool *has)
849: {
850:   PetscErrorCodeFn *fptr = NULL;

852:   PetscFunctionBegin;
853:   PetscAssertPointer(has, 3);
854:   PetscCall(PetscObjectQueryFunction(obj, name, &fptr));
855:   *has = fptr ? PETSC_TRUE : PETSC_FALSE;
856:   PetscFunctionReturn(PETSC_SUCCESS);
857: }

859: struct _p_PetscContainer {
860:   PETSCHEADER(int);
861:   void              *ctx;
862:   PetscCtxDestroyFn *ctxdestroy;
863:   PetscErrorCode (*userdestroy_deprecated)(void *);
864: };

866: /*@C
867:   PetscContainerGetPointer - Gets the pointer value contained in the container that was provided with `PetscContainerSetPointer()`

869:   Not Collective, No Fortran Support

871:   Input Parameter:
872: . obj - the object created with `PetscContainerCreate()`

874:   Output Parameter:
875: . ptr - the pointer value

877:   Level: advanced

879: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscObject`,
880:           `PetscContainerSetPointer()`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
881: @*/
882: PetscErrorCode PetscContainerGetPointer(PetscContainer obj, PetscCtxRt ptr)
883: {
884:   PetscFunctionBegin;
886:   PetscAssertPointer(ptr, 2);
887:   *(void **)ptr = obj->ctx;
888:   PetscFunctionReturn(PETSC_SUCCESS);
889: }

891: /*@C
892:   PetscContainerSetPointer - Sets the pointer value contained in the container.

894:   Logically Collective, No Fortran Support

896:   Input Parameters:
897: + obj - the object created with `PetscContainerCreate()`
898: - ptr - the pointer value

900:   Level: advanced

902: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObject`,
903:           `PetscContainerGetPointer()`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
904: @*/
905: PetscErrorCode PetscContainerSetPointer(PetscContainer obj, void *ptr)
906: {
907:   PetscFunctionBegin;
909:   if (ptr) PetscAssertPointer(ptr, 2);
910:   obj->ctx = ptr;
911:   PetscFunctionReturn(PETSC_SUCCESS);
912: }

914: /*@C
915:   PetscContainerDestroy - Destroys a PETSc container object.

917:   Collective, No Fortran Support

919:   Input Parameter:
920: . obj - an object that was created with `PetscContainerCreate()`

922:   Level: advanced

924:   Note:
925:   If `PetscContainerSetCtxDestroy()` was used to provide a user destroy object for the data provided with `PetscContainerSetPointer()`
926:   then that function is called to destroy the data.

928: .seealso: `PetscContainerCreate()`, `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
929: @*/
930: PetscErrorCode PetscContainerDestroy(PetscContainer *obj)
931: {
932:   PetscFunctionBegin;
933:   if (!*obj) PetscFunctionReturn(PETSC_SUCCESS);
935:   if (--((PetscObject)*obj)->refct > 0) {
936:     *obj = NULL;
937:     PetscFunctionReturn(PETSC_SUCCESS);
938:   }
939:   if ((*obj)->ctxdestroy) PetscCall((*(*obj)->ctxdestroy)(&(*obj)->ctx));
940:   else if ((*obj)->userdestroy_deprecated) PetscCall((*(*obj)->userdestroy_deprecated)((*obj)->ctx));
941:   PetscCall(PetscHeaderDestroy(obj));
942:   PetscFunctionReturn(PETSC_SUCCESS);
943: }

945: /*@C
946:   PetscContainerSetCtxDestroy - Sets the destroy function for the data provided to the `PetscContainer` with `PetscContainerSetPointer()`

948:   Logically Collective, No Fortran Support

950:   Input Parameters:
951: + obj - an object that was created with `PetscContainerCreate()`
952: - des - name of the ctx destroy function, see `PetscCtxDestroyFn` for its calling sequence

954:   Level: advanced

956:   Note:
957:   Use `PetscCtxDestroyDefault()` if the memory was obtained by calling `PetscMalloc()` or one of its variants for single memory allocation.

959: .seealso: `PetscContainerDestroy()`, `PetscContainerUserDestroyDefault()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc()`, `PetscCalloc1()`, `PetscObject`,
960:           `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
961: @*/
962: PetscErrorCode PetscContainerSetCtxDestroy(PetscContainer obj, PetscCtxDestroyFn *des)
963: {
964:   PetscFunctionBegin;
966:   obj->ctxdestroy = des;
967:   PetscFunctionReturn(PETSC_SUCCESS);
968: }

970: /*@C
971:   PetscContainerSetUserDestroy - Sets the destroy function for the data provided to the `PetscContainer` with `PetscContainerSetPointer()`

973:   Logically Collective, No Fortran Support

975:   Input Parameters:
976: + obj - an object that was created with `PetscContainerCreate()`
977: - des - name of the ctx destroy function

979:   Level: advanced

981:   Notes:
982:   Deprecated, use `PetscContainerSetCtxDestroy()`

984: .seealso: `PetscContainerSetCtxDestroy()`, `PetscContainerDestroy()`, `PetscContainerUserDestroyDefault()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc()`, `PetscCalloc1()`, `PetscObject`,
985:           `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
986: @*/
987: PetscErrorCode PetscContainerSetUserDestroy(PetscContainer obj, PetscErrorCode (*des)(void *))
988: {
989:   PetscFunctionBegin;
991:   obj->userdestroy_deprecated = des;
992:   PetscFunctionReturn(PETSC_SUCCESS);
993: }

995: PetscClassId PETSC_CONTAINER_CLASSID;

997: /*@C
998:   PetscContainerCreate - Creates a PETSc object that has room to hold a single pointer.

1000:   Collective, No Fortran Support

1002:   Input Parameter:
1003: . comm - MPI communicator that shares the object

1005:   Output Parameter:
1006: . container - the container created

1008:   Level: advanced

1010:   Notes:
1011:   This allows one to attach any type of data (accessible through a pointer) with the
1012:   `PetscObjectCompose()` function to a `PetscObject`. The data item itself is attached by a
1013:   call to `PetscContainerSetPointer()`.

1015: .seealso: `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1016:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
1017: @*/
1018: PetscErrorCode PetscContainerCreate(MPI_Comm comm, PetscContainer *container)
1019: {
1020:   PetscFunctionBegin;
1021:   PetscAssertPointer(container, 2);
1022:   PetscCall(PetscSysInitializePackage());
1023:   PetscCall(PetscHeaderCreate(*container, PETSC_CONTAINER_CLASSID, "PetscContainer", "Container", "Sys", comm, PetscContainerDestroy, NULL));
1024:   PetscFunctionReturn(PETSC_SUCCESS);
1025: }

1027: /*@C
1028:   PetscObjectContainerCompose - Creates a `PetscContainer`, provides all of its values and composes it with a `PetscObject`

1030:   Collective

1032:   Input Parameters:
1033: + obj     - the `PetscObject`
1034: . name    - the name for the composed container
1035: . pointer - the pointer to the data
1036: - destroy - the routine to destroy the container's data, see `PetscCtxDestroyFn` for its calling sequence; use `PetscCtxDestroyDefault()` if a `PetscFree()` frees the data

1038:   Level: advanced

1040:   Notes:
1041:   This allows one to attach any type of data (accessible through a pointer) with the
1042:   `PetscObjectCompose()` function to a `PetscObject`. The data item itself is attached by a
1043:   call to `PetscContainerSetPointer()`.

1045: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1046:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerQuery()`
1047: @*/
1048: PetscErrorCode PetscObjectContainerCompose(PetscObject obj, const char *name, void *pointer, PetscCtxDestroyFn *destroy)
1049: {
1050:   PetscContainer container;

1052:   PetscFunctionBegin;
1053:   PetscCall(PetscContainerCreate(PetscObjectComm(obj), &container));
1054:   PetscCall(PetscContainerSetPointer(container, pointer));
1055:   if (destroy) PetscCall(PetscContainerSetCtxDestroy(container, destroy));
1056:   PetscCall(PetscObjectCompose(obj, name, (PetscObject)container));
1057:   PetscCall(PetscContainerDestroy(&container));
1058:   PetscFunctionReturn(PETSC_SUCCESS);
1059: }

1061: /*@C
1062:   PetscObjectContainerQuery - Accesses the pointer in a container composed to a `PetscObject` with `PetscObjectContainerCompose()`

1064:   Collective

1066:   Input Parameters:
1067: + obj  - the `PetscObject`
1068: - name - the name for the composed container

1070:   Output Parameter:
1071: . ptr - the pointer to the data

1073:   Level: advanced

1075: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1076:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`
1077: @*/
1078: PetscErrorCode PetscObjectContainerQuery(PetscObject obj, const char *name, PetscCtxRt ptr)
1079: {
1080:   PetscContainer container;

1082:   PetscFunctionBegin;
1083:   PetscCall(PetscObjectQuery(obj, name, (PetscObject *)&container));
1084:   if (container) PetscCall(PetscContainerGetPointer(container, ptr));
1085:   else *(void **)ptr = NULL;
1086:   PetscFunctionReturn(PETSC_SUCCESS);
1087: }

1089: /*@
1090:   PetscObjectSetFromOptions - Sets generic parameters from user options.

1092:   Collective

1094:   Input Parameter:
1095: . obj - the `PetscObject`

1097:   Level: beginner

1099:   Note:
1100:   We have no generic options at present, so this does nothing.

1102: .seealso: `PetscObjectSetOptionsPrefix()`, `PetscObjectGetOptionsPrefix()`, `PetscObject`
1103: @*/
1104: PetscErrorCode PetscObjectSetFromOptions(PetscObject obj)
1105: {
1106:   PetscFunctionBegin;
1108:   PetscFunctionReturn(PETSC_SUCCESS);
1109: }

1111: /*@
1112:   PetscObjectSetUp - Sets up the internal data structures for later use of the object

1114:   Collective

1116:   Input Parameter:
1117: . obj - the `PetscObject`

1119:   Level: advanced

1121:   Note:
1122:   This does nothing at present.

1124: .seealso: `PetscObjectDestroy()`, `PetscObject`
1125: @*/
1126: PetscErrorCode PetscObjectSetUp(PetscObject obj)
1127: {
1128:   PetscFunctionBegin;
1130:   PetscFunctionReturn(PETSC_SUCCESS);
1131: }

1133: /*MC
1134:   PetscObjectIsNull - returns true if the given PETSc object is a null object

1136:   Fortran only

1138:   Synopsis:
1139:   #include <petsc/finclude/petscsys.h>
1140:   PetscBool PetscObjectIsNull(PetscObject obj)

1142:   Logically Collective

1144:   Input Parameter:
1145: . obj  - the PETSc object

1147:   Level: beginner

1149:   Example Usage:
1150: .vb
1151:   if (PetscObjectIsNull(dm)) then
1152:   if (.not. PetscObjectIsNull(dm)) then
1153: .ve

1155:   Note:
1156:   Code such as
1157: .vb
1158:   if (dm == PETSC_NULL_DM) then
1159: .ve
1160:   is not allowed.

1162: .seealso: `PetscObject`, `PETSC_NULL_OBJECT`, `PETSC_NULL_VEC`, `PETSC_NULL_VEC_ARRAY`, `PetscObjectNullify()`
1163: M*/

1165: /*MC
1166:   PetscObjectNullify - sets a PETSc object, such as `Vec`, back to the state it had when it was declared, so it
1167:   can be used in a creation routine, such as `VecCreate()`

1169:   Fortran only

1171:   Synopsis:
1172:   #include <petsc/finclude/petscsys.h>
1173:   PetscObjectNullify(PetscObject obj)

1175:   Logically Collective

1177:   Input Parameter:
1178: . obj  - the PETSc object

1180:   Level: beginner

1182:   Example Usage:
1183: .vb
1184:   Vec x, y

1186:   VecCreate(PETSC_COMM_WORLD, x, ierr)
1187:   ...
1188:   y = x
1189:   ...
1190:   PetscObjectNullify(y)
1191: .ve
1192:   You should not call `VecDestroy()` on `y` because that will destroy `x` since the assignment `y = x` does
1193:   not increase the reference count of `x`

1195:   Note:
1196:   Code such as
1197: .vb
1198:   y = PETSC_NULL_VEC
1199: .ve
1200:   is not allowed.

1202: .seealso: `PetscObject`, `PETSC_NULL_OBJECT`, `PETSC_NULL_VEC`, `PETSC_NULL_VEC_ARRAY`, `PetscObjectIsNull()`
1203: M*/

1205: /*MC
1206:   PetscObjectCast - Casts a `PetscObject` to the base `PetscObject` type in function calls

1208:   Fortran only

1210:   Synopsis:
1211:   use petscsys

1213:   Level: beginner

1215:   Example Usage:
1216:   PetscFE fe
1217: .vb
1218:   PetscCallA(DMAddField(dm, 0, PetscObjectCast(fe),ierr)
1219: .ve

1221: .seealso: `PetscObject`, `PetscObjectSpecificCast()`
1222: M*/

1224: /*MC
1225:   PetscObjectSpecificCast - Casts a `PetscObject` to any specific `PetscObject`

1227:   Fortran only

1229:   Synopsis:
1230:   use petscsys

1232:   Level: beginner

1234:   Example Usage:
1235:   PetscObject obj
1236:   PetscFE     fe
1237: .vb
1238:   PetscCallA(PetscDSGetDiscretization(ds, 0, obj, ierr)
1239:   PetscObjectSpecificCast(fe,obj)
1240: .ve

1242: .seealso: `PetscObject`, `PetscObjectCast()`
1243: M*/

1245: /*MC
1246:   PetscEnumCase - `case()` statement for a PETSc enum variable or value

1248:   Fortran only

1250:   Synopsis:
1251:   #include <petsc/finclude/petscsys.h>
1252:   PetscEnumCase(PetscObject enm)

1254:   Input Parameters:
1255: . enum  - the PETSc enum value or variable

1257:   Level: beginner

1259:   Example Usage:
1260: .vb
1261:   DMPolytopeType cellType
1262:   select PetscEnumCase(cellType)
1263:     PetscEnumCase(DM_POLYTOPE_TRIANGLE)
1264:       write(*,*) 'cell is a triangle'
1265:     PetscEnumCase(DM_POLYTOPE_TETRAHEDRON)
1266:       write(*,*) 'cell is a tetrahedron'
1267:     case default
1268:       write(*,*) 'cell is a something else'
1269:   end select
1270: .ve
1271:   is equivalent to
1272: .vb
1273:   DMPolytopeType cellType
1274:   select case(cellType%v)
1275:     case(DM_POLYTOPE_TRIANGLE%v)
1276:       write(*,*) 'cell is a triangle'
1277:     case(DM_POLYTOPE_TETRAHEDRON%v)
1278:       write(*,*) 'cell is a tetrahedron'
1279:     case default
1280:       write(*,*) 'cell is a something else'
1281:   end select
1282: .ve

1284: .seealso: `PetscObject`
1285: M*/