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:   PetscObject h;
429:   PetscBool   flg;

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

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

459: /*@
460:   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

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

465:   Level: developer

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

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

481: /*@
482:   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.

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

488:   Level: developer

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

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

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

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

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

514:   Not Collective

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

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

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

531:   Level: developer

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

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

554:   Not Collective

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

560:   Level: developer

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

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

576:   Not Collective

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

581:   Level: developer

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

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

602:   Logically Collective

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

607:   Level: advanced

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

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

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

626:   Not Collective

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

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

635:   Level: advanced

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

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

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

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

659:   Level: advanced

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

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

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

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

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

693:   Not Collective

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

702:   Level: advanced

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

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

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

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

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

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

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

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

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

749:   Not Collective

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

758:   Level: advanced

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

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

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

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

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

789:   Logically Collective

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

797:   Level: advanced

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

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

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

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

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

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

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

834:   Logically Collective

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

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

843:   Level: advanced

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

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

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

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

868:   Not Collective, No Fortran Support

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

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

876:   Level: advanced

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

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

893:   Logically Collective, No Fortran Support

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

899:   Level: advanced

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

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

916:   Collective, No Fortran Support

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

921:   Level: advanced

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

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

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

947:   Logically Collective, No Fortran Support

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

953:   Level: advanced

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

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

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

972:   Logically Collective, No Fortran Support

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

978:   Level: advanced

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

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

994: PetscClassId PETSC_CONTAINER_CLASSID;

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

999:   Collective, No Fortran Support

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

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

1007:   Level: advanced

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

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

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

1029:   Collective

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

1037:   Level: advanced

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

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

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

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

1063:   Collective

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

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

1072:   Level: advanced

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

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

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

1091:   Collective

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

1096:   Level: beginner

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

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

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

1113:   Collective

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

1118:   Level: advanced

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

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

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

1135:   Fortran only

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

1141:   Logically Collective

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

1146:   Level: beginner

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

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

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

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

1168:   Fortran only

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

1174:   Logically Collective

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

1179:   Level: beginner

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

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

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

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

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

1207:   Fortran only

1209:   Synopsis:
1210:   use petscsys

1212:   Level: beginner

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

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

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

1226:   Fortran only

1228:   Synopsis:
1229:   use petscsys

1231:   Level: beginner

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

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

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

1247:   Fortran only

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

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

1256:   Level: beginner

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

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