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, numcb[PETSC_FORTRAN_CALLBACK_MAXTYPE];

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:   PetscCall(PetscFortranCallbackGetSizes(src->classid, &numcb[PETSC_FORTRAN_CALLBACK_CLASS], &numcb[PETSC_FORTRAN_CALLBACK_SUBTYPE]));
236:   for (cbtype = PETSC_FORTRAN_CALLBACK_CLASS; cbtype < PETSC_FORTRAN_CALLBACK_MAXTYPE; cbtype++) {
237:     PetscCall(PetscFree(dest->fortrancallback[cbtype]));
238:     PetscCall(PetscCalloc1(numcb[cbtype], &dest->fortrancallback[cbtype]));
239:     PetscCall(PetscArraycpy(dest->fortrancallback[cbtype], src->fortrancallback[cbtype], src->num_fortrancallback[cbtype]));
240:     dest->num_fortrancallback[cbtype] = src->num_fortrancallback[cbtype];
241:   }
242:   PetscFunctionReturn(PETSC_SUCCESS);
243: }

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

248:   Logically Collective, No Fortran Support

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

257:   Level: developer

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

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

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

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

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

291:   Logically Collective, No Fortran Support

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

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

302:   Level: developer

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

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

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

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

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

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

334:   Level: advanced

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

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

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

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

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

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

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

388:   Logically Collective

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

393:   Level: advanced

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

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

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

414:   Not Collective

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

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

423:   Level: advanced

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

432:   PetscFunctionBegin;
433:   PetscAssertPointer(name, 1);
434:   if (obj) *obj = NULL;
435:   for (PetscInt 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: /*MC
824:   PetscObjectQueryFunction - Gets a function associated with a given object.

826:   Synopsis:
827: #include <petscsys.h>
828:   PetscErrorCode PetscObjectQueryFunction(PetscObject obj, const char name[], PetscErrorCodeFn **fptr)

830:   Logically Collective

832:   Input Parameters:
833: + obj  - the PETSc object; this must be cast with (`PetscObject`), for example,
834:          `PetscObjectQueryFunction`((`PetscObject`)ksp,...);
835: - name - name associated with the child function

837:   Output Parameter:
838: . fptr - function pointer

840:   Level: advanced

842: .seealso: `PetscObjectComposeFunction()`, `PetscFunctionListFind()`, `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObject`
843: M*/
844: PETSC_EXTERN PetscErrorCode PetscObjectQueryFunction_Private(PetscObject obj, const char name[], PetscErrorCodeFn **fptr)
845: {
846:   PetscFunctionBegin;
848:   PetscAssertPointer(name, 2);
849:   PetscCall(PetscFunctionListFind_Private(obj->qlist, name, fptr));
850:   PetscFunctionReturn(PETSC_SUCCESS);
851: }

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

856:   Logically Collective

858:   Input Parameters:
859: + obj  - the PETSc object
860: - name - name associated with the child function

862:   Output Parameter:
863: . has - the boolean value

865:   Level: advanced

867: .seealso: `PetscObject`, `PetscObjectComposeFunction()`, `PetscObjectQueryFunction()`
868: @*/
869: PetscErrorCode PetscObjectHasFunction(PetscObject obj, const char name[], PetscBool *has)
870: {
871:   PetscErrorCodeFn *fptr = NULL;

873:   PetscFunctionBegin;
874:   PetscAssertPointer(has, 3);
875:   PetscCall(PetscObjectQueryFunction(obj, name, &fptr));
876:   *has = fptr ? PETSC_TRUE : PETSC_FALSE;
877:   PetscFunctionReturn(PETSC_SUCCESS);
878: }

880: struct _p_PetscContainer {
881:   PETSCHEADER(int);
882:   void              *ctx;
883:   PetscCtxDestroyFn *ctxdestroy;
884:   PetscErrorCode (*userdestroy_deprecated)(void *);
885: };

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

890:   Not Collective, No Fortran Support

892:   Input Parameter:
893: . obj - the object created with `PetscContainerCreate()`

895:   Output Parameter:
896: . ptr - the pointer value

898:   Level: advanced

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

912: /*@C
913:   PetscContainerSetPointer - Sets the pointer value contained in the container.

915:   Logically Collective, No Fortran Support

917:   Input Parameters:
918: + obj - the object created with `PetscContainerCreate()`
919: - ptr - the pointer value

921:   Level: advanced

923: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscObjectCompose()`, `PetscObjectQuery()`, `PetscObject`,
924:           `PetscContainerGetPointer()`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
925: @*/
926: PetscErrorCode PetscContainerSetPointer(PetscContainer obj, void *ptr)
927: {
928:   PetscFunctionBegin;
930:   if (ptr) PetscAssertPointer(ptr, 2);
931:   obj->ctx = ptr;
932:   PetscFunctionReturn(PETSC_SUCCESS);
933: }

935: /*@C
936:   PetscContainerDestroy - Destroys a PETSc container object.

938:   Collective, No Fortran Support

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

943:   Level: advanced

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

949: .seealso: `PetscContainerCreate()`, `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
950: @*/
951: PetscErrorCode PetscContainerDestroy(PetscContainer *obj)
952: {
953:   PetscFunctionBegin;
954:   if (!*obj) PetscFunctionReturn(PETSC_SUCCESS);
956:   if (--((PetscObject)*obj)->refct > 0) {
957:     *obj = NULL;
958:     PetscFunctionReturn(PETSC_SUCCESS);
959:   }
960:   if ((*obj)->ctxdestroy) PetscCall((*(*obj)->ctxdestroy)(&(*obj)->ctx));
961:   else if ((*obj)->userdestroy_deprecated) PetscCall((*(*obj)->userdestroy_deprecated)((*obj)->ctx));
962:   PetscCall(PetscHeaderDestroy(obj));
963:   PetscFunctionReturn(PETSC_SUCCESS);
964: }

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

969:   Logically Collective, No Fortran Support

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

975:   Level: advanced

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

980: .seealso: `PetscContainerDestroy()`, `PetscContainerUserDestroyDefault()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc()`, `PetscCalloc1()`, `PetscObject`,
981:           `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
982: @*/
983: PetscErrorCode PetscContainerSetCtxDestroy(PetscContainer obj, PetscCtxDestroyFn *des)
984: {
985:   PetscFunctionBegin;
987:   obj->ctxdestroy = des;
988:   PetscFunctionReturn(PETSC_SUCCESS);
989: }

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

994:   Logically Collective, No Fortran Support

996:   Input Parameters:
997: + obj - an object that was created with `PetscContainerCreate()`
998: - des - name of the ctx destroy function

1000:   Level: advanced

1002:   Notes:
1003:   Deprecated, use `PetscContainerSetCtxDestroy()`

1005: .seealso: `PetscContainerSetCtxDestroy()`, `PetscContainerDestroy()`, `PetscContainerUserDestroyDefault()`, `PetscMalloc()`, `PetscMalloc1()`, `PetscCalloc()`, `PetscCalloc1()`, `PetscObject`,
1006:           `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
1007: @*/
1008: PetscErrorCode PetscContainerSetUserDestroy(PetscContainer obj, PetscErrorCode (*des)(void *))
1009: {
1010:   PetscFunctionBegin;
1012:   obj->userdestroy_deprecated = des;
1013:   PetscFunctionReturn(PETSC_SUCCESS);
1014: }

1016: PetscClassId PETSC_CONTAINER_CLASSID;

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

1021:   Collective, No Fortran Support

1023:   Input Parameter:
1024: . comm - MPI communicator that shares the object

1026:   Output Parameter:
1027: . container - the container created

1029:   Level: advanced

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

1036: .seealso: `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1037:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`, `PetscObjectContainerQuery()`
1038: @*/
1039: PetscErrorCode PetscContainerCreate(MPI_Comm comm, PetscContainer *container)
1040: {
1041:   PetscFunctionBegin;
1042:   PetscAssertPointer(container, 2);
1043:   PetscCall(PetscSysInitializePackage());
1044:   PetscCall(PetscHeaderCreate(*container, PETSC_CONTAINER_CLASSID, "PetscContainer", "Container", "Sys", comm, PetscContainerDestroy, NULL));
1045:   PetscFunctionReturn(PETSC_SUCCESS);
1046: }

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

1051:   Collective

1053:   Input Parameters:
1054: + obj     - the `PetscObject`
1055: . name    - the name for the composed container
1056: . pointer - the pointer to the data
1057: - destroy - the routine to destroy the container's data, see `PetscCtxDestroyFn` for its calling sequence; use `PetscCtxDestroyDefault()` if a `PetscFree()` frees the data

1059:   Level: advanced

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

1066: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1067:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerQuery()`
1068: @*/
1069: PetscErrorCode PetscObjectContainerCompose(PetscObject obj, const char *name, void *pointer, PetscCtxDestroyFn *destroy)
1070: {
1071:   PetscContainer container;

1073:   PetscFunctionBegin;
1074:   PetscCall(PetscContainerCreate(PetscObjectComm(obj), &container));
1075:   PetscCall(PetscContainerSetPointer(container, pointer));
1076:   if (destroy) PetscCall(PetscContainerSetCtxDestroy(container, destroy));
1077:   PetscCall(PetscObjectCompose(obj, name, (PetscObject)container));
1078:   PetscCall(PetscContainerDestroy(&container));
1079:   PetscFunctionReturn(PETSC_SUCCESS);
1080: }

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

1085:   Collective

1087:   Input Parameters:
1088: + obj  - the `PetscObject`
1089: - name - the name for the composed container

1091:   Output Parameter:
1092: . ptr - the pointer to the data

1094:   Level: advanced

1096: .seealso: `PetscContainerCreate()`, `PetscContainerDestroy()`, `PetscContainerSetPointer()`, `PetscContainerGetPointer()`, `PetscObjectCompose()`, `PetscObjectQuery()`,
1097:           `PetscContainerSetCtxDestroy()`, `PetscObject`, `PetscObjectContainerCompose()`
1098: @*/
1099: PetscErrorCode PetscObjectContainerQuery(PetscObject obj, const char *name, PetscCtxRt ptr)
1100: {
1101:   PetscContainer container;

1103:   PetscFunctionBegin;
1104:   PetscCall(PetscObjectQuery(obj, name, (PetscObject *)&container));
1105:   if (container) PetscCall(PetscContainerGetPointer(container, ptr));
1106:   else *(void **)ptr = NULL;
1107:   PetscFunctionReturn(PETSC_SUCCESS);
1108: }

1110: /*@
1111:   PetscObjectSetFromOptions - Sets generic parameters from user options.

1113:   Collective

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

1118:   Level: beginner

1120:   Note:
1121:   We have no generic options at present, so this does nothing.

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

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

1135:   Collective

1137:   Input Parameter:
1138: . obj - the `PetscObject`

1140:   Level: advanced

1142:   Note:
1143:   This does nothing at present.

1145: .seealso: `PetscObjectDestroy()`, `PetscObject`
1146: @*/
1147: PetscErrorCode PetscObjectSetUp(PetscObject obj)
1148: {
1149:   PetscFunctionBegin;
1151:   PetscFunctionReturn(PETSC_SUCCESS);
1152: }

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

1157:   Fortran only

1159:   Synopsis:
1160:   #include <petsc/finclude/petscsys.h>
1161:   PetscBool PetscObjectIsNull(PetscObject obj)

1163:   Logically Collective

1165:   Input Parameter:
1166: . obj  - the PETSc object

1168:   Level: beginner

1170:   Example Usage:
1171: .vb
1172:   if (PetscObjectIsNull(dm)) then
1173:   if (.not. PetscObjectIsNull(dm)) then
1174: .ve

1176:   Note:
1177:   Code such as
1178: .vb
1179:   if (dm == PETSC_NULL_DM) then
1180: .ve
1181:   is not allowed.

1183: .seealso: `PetscObject`, `PETSC_NULL_OBJECT`, `PETSC_NULL_VEC`, `PETSC_NULL_VEC_ARRAY`, `PetscObjectNullify()`
1184: M*/

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

1190:   Fortran only

1192:   Synopsis:
1193:   #include <petsc/finclude/petscsys.h>
1194:   PetscObjectNullify(PetscObject obj)

1196:   Logically Collective

1198:   Input Parameter:
1199: . obj  - the PETSc object

1201:   Level: beginner

1203:   Example Usage:
1204: .vb
1205:   Vec x, y

1207:   VecCreate(PETSC_COMM_WORLD, x, ierr)
1208:   ...
1209:   y = x
1210:   ...
1211:   PetscObjectNullify(y)
1212: .ve
1213:   You should not call `VecDestroy()` on `y` because that will destroy `x` since the assignment `y = x` does
1214:   not increase the reference count of `x`

1216:   Note:
1217:   Code such as
1218: .vb
1219:   y = PETSC_NULL_VEC
1220: .ve
1221:   is not allowed.

1223: .seealso: `PetscObject`, `PETSC_NULL_OBJECT`, `PETSC_NULL_VEC`, `PETSC_NULL_VEC_ARRAY`, `PetscObjectIsNull()`
1224: M*/

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

1229:   Fortran only

1231:   Synopsis:
1232:   use petscsys

1234:   Level: beginner

1236:   Example Usage:
1237:   PetscFE fe
1238: .vb
1239:   PetscCallA(DMAddField(dm, 0, PetscObjectCast(fe),ierr)
1240: .ve

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

1245: /*MC
1246:   PetscObjectSpecificCast - Casts a `PetscObject` to any specific `PetscObject`

1248:   Fortran only

1250:   Synopsis:
1251:   use petscsys

1253:   Level: beginner

1255:   Example Usage:
1256:   PetscObject obj
1257:   PetscFE     fe
1258: .vb
1259:   PetscCallA(PetscDSGetDiscretization(ds, 0, obj, ierr)
1260:   PetscObjectSpecificCast(fe,obj)
1261: .ve

1263: .seealso: `PetscObject`, `PetscObjectCast()`
1264: M*/

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

1269:   Fortran only

1271:   Synopsis:
1272:   #include <petsc/finclude/petscsys.h>
1273:   PetscEnumCase(PetscObject enm)

1275:   Input Parameters:
1276: . enum  - the PETSc enum value or variable

1278:   Level: beginner

1280:   Example Usage:
1281: .vb
1282:   DMPolytopeType cellType
1283:   select PetscEnumCase(cellType)
1284:     PetscEnumCase(DM_POLYTOPE_TRIANGLE)
1285:       write(*,*) 'cell is a triangle'
1286:     PetscEnumCase(DM_POLYTOPE_TETRAHEDRON)
1287:       write(*,*) 'cell is a tetrahedron'
1288:     case default
1289:       write(*,*) 'cell is a something else'
1290:   end select
1291: .ve
1292:   is equivalent to
1293: .vb
1294:   DMPolytopeType cellType
1295:   select case(cellType%v)
1296:     case(DM_POLYTOPE_TRIANGLE%v)
1297:       write(*,*) 'cell is a triangle'
1298:     case(DM_POLYTOPE_TETRAHEDRON%v)
1299:       write(*,*) 'cell is a tetrahedron'
1300:     case default
1301:       write(*,*) 'cell is a something else'
1302:   end select
1303: .ve

1305: .seealso: `PetscObject`
1306: M*/