Actual source code: draw.c
petsc-3.13.6 2020-09-29
Report Typos and Errors

      2: /*
      3:        Provides the calling sequences for all the basic PetscDraw routines.
      4: */
      5:  #include <petsc/private/drawimpl.h>
      6:  #include <petscviewer.h>

      8: PetscClassId PETSC_DRAW_CLASSID;

     10: static PetscBool PetscDrawPackageInitialized = PETSC_FALSE;
     11: /*@C
     12:   PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is
     13:   called from PetscFinalize().

     15:   Level: developer

     17: .seealso: PetscFinalize()
     18: @*/
     19: PetscErrorCode  PetscDrawFinalizePackage(void)
     20: {

     24:   PetscFunctionListDestroy(&PetscDrawList);
     25:   PetscDrawPackageInitialized = PETSC_FALSE;
     26:   PetscDrawRegisterAllCalled  = PETSC_FALSE;
     27:   return(0);
     28: }

     30: /*@C
     31:   PetscInitializeDrawPackage - This function initializes everything in the PetscDraw package. It is called
     32:   from PetscDLLibraryRegister_petsc() when using dynamic libraries, and on the call to PetscInitialize()
     33:   when using shared or static libraries.

     35:   Level: developer

     37: .seealso: PetscInitialize()
     38: @*/
     39: PetscErrorCode  PetscDrawInitializePackage(void)
     40: {
     41:   char           logList[256];
     42:   PetscBool      opt,pkg;

     46:   if (PetscDrawPackageInitialized) return(0);
     47:   PetscDrawPackageInitialized = PETSC_TRUE;
     48:   /* Register Classes */
     49:   PetscClassIdRegister("Draw",&PETSC_DRAW_CLASSID);
     50:   PetscClassIdRegister("Draw Axis",&PETSC_DRAWAXIS_CLASSID);
     51:   PetscClassIdRegister("Line Graph",&PETSC_DRAWLG_CLASSID);
     52:   PetscClassIdRegister("Histogram",&PETSC_DRAWHG_CLASSID);
     53:   PetscClassIdRegister("Bar Graph",&PETSC_DRAWBAR_CLASSID);
     54:   PetscClassIdRegister("Scatter Plot",&PETSC_DRAWSP_CLASSID);
     55:   /* Register Constructors */
     56:   PetscDrawRegisterAll();
     57:   /* Process Info */
     58:   {
     59:     PetscClassId  classids[6];

     61:     classids[0] = PETSC_DRAW_CLASSID;
     62:     classids[1] = PETSC_DRAWAXIS_CLASSID;
     63:     classids[2] = PETSC_DRAWLG_CLASSID;
     64:     classids[3] = PETSC_DRAWHG_CLASSID;
     65:     classids[4] = PETSC_DRAWBAR_CLASSID;
     66:     classids[5] = PETSC_DRAWSP_CLASSID;
     67:     PetscInfoProcessClass("draw", 6, classids);
     68:   }
     69:   /* Process summary exclusions */
     70:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
     71:   if (opt) {
     72:     PetscStrInList("draw",logList,',',&pkg);
     73:     if (pkg) {
     74:       PetscLogEventExcludeClass(PETSC_DRAW_CLASSID);
     75:       PetscLogEventExcludeClass(PETSC_DRAWAXIS_CLASSID);
     76:       PetscLogEventExcludeClass(PETSC_DRAWLG_CLASSID);
     77:       PetscLogEventExcludeClass(PETSC_DRAWHG_CLASSID);
     78:       PetscLogEventExcludeClass(PETSC_DRAWBAR_CLASSID);
     79:       PetscLogEventExcludeClass(PETSC_DRAWSP_CLASSID);
     80:     }
     81:   }
     82:   /* Register package finalizer */
     83:   PetscRegisterFinalize(PetscDrawFinalizePackage);
     84:   return(0);
     85: }

     87: /*@
     88:    PetscDrawResizeWindow - Allows one to resize a window from a program.

     90:    Collective on PetscDraw

     92:    Input Parameter:
     93: +  draw - the window
     94: -  w,h - the new width and height of the window

     96:    Level: intermediate

     98: .seealso: PetscDrawCheckResizedWindow()
     99: @*/
    100: PetscErrorCode  PetscDrawResizeWindow(PetscDraw draw,int w,int h)
    101: {

    108:   if (draw->ops->resizewindow) {
    109:     (*draw->ops->resizewindow)(draw,w,h);
    110:   }
    111:   return(0);
    112: }

    114: /*@
    115:    PetscDrawGetWindowSize - Gets the size of the window.

    117:    Not collective

    119:    Input Parameter:
    120: .  draw - the window

    122:    Output Parameters:
    123: .  w,h - the window width and height

    125:    Level: intermediate

    127: .seealso: PetscDrawResizeWindow(), PetscDrawCheckResizedWindow()
    128: @*/
    129: PetscErrorCode  PetscDrawGetWindowSize(PetscDraw draw,int *w,int *h)
    130: {
    135:   if (w) *w = draw->w;
    136:   if (h) *h = draw->h;
    137:   return(0);
    138: }

    140: /*@
    141:    PetscDrawCheckResizedWindow - Checks if the user has resized the window.

    143:    Collective on PetscDraw

    145:    Input Parameter:
    146: .  draw - the window

    148:    Level: advanced

    150: .seealso: PetscDrawResizeWindow()

    152: @*/
    153: PetscErrorCode  PetscDrawCheckResizedWindow(PetscDraw draw)
    154: {

    159:   if (draw->ops->checkresizedwindow) {
    160:     (*draw->ops->checkresizedwindow)(draw);
    161:   }
    162:   return(0);
    163: }

    165: /*@C
    166:    PetscDrawGetTitle - Gets pointer to title of a PetscDraw context.

    168:    Not collective

    170:    Input Parameter:
    171: .  draw - the graphics context

    173:    Output Parameter:
    174: .  title - the title

    176:    Level: intermediate

    178: .seealso: PetscDrawSetTitle()
    179: @*/
    180: PetscErrorCode  PetscDrawGetTitle(PetscDraw draw,const char *title[])
    181: {
    185:   *title = draw->title;
    186:   return(0);
    187: }

    189: /*@C
    190:    PetscDrawSetTitle - Sets the title of a PetscDraw context.

    192:    Collective on PetscDraw

    194:    Input Parameters:
    195: +  draw - the graphics context
    196: -  title - the title

    198:    Level: intermediate

    200:    Note: The title is positioned in the windowing system title bar for the window. Hence it will not be saved with -draw_save
    201:    in the image.

    203:    A copy of the string is made, so you may destroy the
    204:    title string after calling this routine.

    206:    You can use PetscDrawAxisSetLabels() to indicate a title within the window

    208: .seealso: PetscDrawGetTitle(), PetscDrawAppendTitle()
    209: @*/
    210: PetscErrorCode  PetscDrawSetTitle(PetscDraw draw,const char title[])
    211: {

    217:   PetscFree(draw->title);
    218:   PetscStrallocpy(title,&draw->title);
    219:   if (draw->ops->settitle) {
    220:     (*draw->ops->settitle)(draw,draw->title);
    221:   }
    222:   return(0);
    223: }

    225: /*@C
    226:    PetscDrawAppendTitle - Appends to the title of a PetscDraw context.

    228:    Collective on PetscDraw

    230:    Input Parameters:
    231: +  draw - the graphics context
    232: -  title - the title

    234:    Note:
    235:    A copy of the string is made, so you may destroy the
    236:    title string after calling this routine.

    238:    Level: advanced

    240: .seealso: PetscDrawSetTitle(), PetscDrawGetTitle()
    241: @*/
    242: PetscErrorCode  PetscDrawAppendTitle(PetscDraw draw,const char title[])
    243: {

    249:   if (!title || !title[0]) return(0);

    251:   if (draw->title) {
    252:     size_t len1,len2;
    253:     char   *newtitle;
    254:     PetscStrlen(title,&len1);
    255:     PetscStrlen(draw->title,&len2);
    256:     PetscMalloc1(len1 + len2 + 1,&newtitle);
    257:     PetscStrcpy(newtitle,draw->title);
    258:     PetscStrcat(newtitle,title);
    259:     PetscFree(draw->title);
    260:     draw->title = newtitle;
    261:   } else {
    262:     PetscStrallocpy(title,&draw->title);
    263:   }
    264:   if (draw->ops->settitle) {
    265:     (*draw->ops->settitle)(draw,draw->title);
    266:   }
    267:   return(0);
    268: }

    270: static PetscErrorCode PetscDrawDestroy_Private(PetscDraw draw)
    271: {

    275:   if (!draw->ops->save && !draw->ops->getimage) return(0);
    276:   PetscDrawSaveMovie(draw);
    277:   if (draw->savefinalfilename) {
    278:     draw->savesinglefile = PETSC_TRUE;
    279:     PetscDrawSetSave(draw,draw->savefinalfilename);
    280:     PetscDrawSave(draw);
    281:   }
    282:   PetscBarrier((PetscObject)draw);
    283:   return(0);
    284: }

    286: /*@
    287:    PetscDrawDestroy - Deletes a draw context.

    289:    Collective on PetscDraw

    291:    Input Parameters:
    292: .  draw - the drawing context

    294:    Level: beginner

    296: .seealso: PetscDrawCreate()

    298: @*/
    299: PetscErrorCode  PetscDrawDestroy(PetscDraw *draw)
    300: {

    304:   if (!*draw) return(0);
    306:   if (--((PetscObject)(*draw))->refct > 0) return(0);

    308:   if ((*draw)->pause == -2) {
    309:     (*draw)->pause = -1;
    310:     PetscDrawPause(*draw);
    311:   }

    313:   /* if memory was published then destroy it */
    314:   PetscObjectSAWsViewOff((PetscObject)*draw);

    316:   PetscDrawDestroy_Private(*draw);

    318:   if ((*draw)->ops->destroy) {
    319:     (*(*draw)->ops->destroy)(*draw);
    320:   }
    321:   PetscDrawDestroy(&(*draw)->popup);
    322:   PetscFree((*draw)->title);
    323:   PetscFree((*draw)->display);
    324:   PetscFree((*draw)->savefilename);
    325:   PetscFree((*draw)->saveimageext);
    326:   PetscFree((*draw)->savemovieext);
    327:   PetscFree((*draw)->savefinalfilename);
    328:   PetscHeaderDestroy(draw);
    329:   return(0);
    330: }

    332: /*@
    333:    PetscDrawGetPopup - Creates a popup window associated with a PetscDraw window.

    335:    Collective on PetscDraw

    337:    Input Parameter:
    338: .  draw - the original window

    340:    Output Parameter:
    341: .  popup - the new popup window

    343:    Level: advanced

    345: .seealso: PetscDrawScalePopup(), PetscDrawCreate()

    347: @*/
    348: PetscErrorCode  PetscDrawGetPopup(PetscDraw draw,PetscDraw *popup)
    349: {

    356:   if (draw->popup) *popup = draw->popup;
    357:   else if (draw->ops->getpopup) {
    358:     (*draw->ops->getpopup)(draw,popup);
    359:     if (*popup) {
    360:       PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
    361:       (*popup)->pause = 0.0;
    362:       PetscDrawSetFromOptions(*popup);
    363:     }
    364:   } else *popup = NULL;
    365:   return(0);
    366: }

    368: /*@C
    369:   PetscDrawSetDisplay - Sets the display where a PetscDraw object will be displayed

    371:   Input Parameter:
    372: + draw - the drawing context
    373: - display - the X windows display

    375:   Level: advanced

    377: .seealso: PetscDrawCreate()

    379: @*/
    380: PetscErrorCode  PetscDrawSetDisplay(PetscDraw draw,const char display[])
    381: {

    385:   PetscFree(draw->display);
    386:   PetscStrallocpy(display,&draw->display);
    387:   return(0);
    388: }

    391: /*@
    392:    PetscDrawSetDoubleBuffer - Sets a window to be double buffered.

    394:    Logically Collective on PetscDraw

    396:    Input Parameter:
    397: .  draw - the drawing context

    399:    Level: intermediate

    401: @*/
    402: PetscErrorCode  PetscDrawSetDoubleBuffer(PetscDraw draw)
    403: {

    408:   if (draw->ops->setdoublebuffer) {
    409:     (*draw->ops->setdoublebuffer)(draw);
    410:   }
    411:   return(0);
    412: }

    414: /*@C
    415:    PetscDrawGetSingleton - Gain access to a PetscDraw object as if it were owned
    416:         by the one process.

    418:    Collective on PetscDraw

    420:    Input Parameter:
    421: .  draw - the original window

    423:    Output Parameter:
    424: .  sdraw - the singleton window

    426:    Level: advanced

    428: .seealso: PetscDrawRestoreSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

    430: @*/
    431: PetscErrorCode  PetscDrawGetSingleton(PetscDraw draw,PetscDraw *sdraw)
    432: {
    434:   PetscMPIInt    size;

    440:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
    441:   if (size == 1) {
    442:     PetscObjectReference((PetscObject)draw);
    443:     *sdraw = draw;
    444:   } else {
    445:     if (draw->ops->getsingleton) {
    446:       (*draw->ops->getsingleton)(draw,sdraw);
    447:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get singleton for this type %s of draw object",((PetscObject)draw)->type_name);
    448:   }
    449:   return(0);
    450: }

    452: /*@C
    453:    PetscDrawRestoreSingleton - Remove access to a PetscDraw object as if it were owned
    454:         by the one process.

    456:    Collective on PetscDraw

    458:    Input Parameters:
    459: +  draw - the original window
    460: -  sdraw - the singleton window

    462:    Level: advanced

    464: .seealso: PetscDrawGetSingleton(), PetscViewerGetSingleton(), PetscViewerRestoreSingleton()

    466: @*/
    467: PetscErrorCode  PetscDrawRestoreSingleton(PetscDraw draw,PetscDraw *sdraw)
    468: {
    470:   PetscMPIInt    size;

    477:   MPI_Comm_size(PetscObjectComm((PetscObject)draw),&size);
    478:   if (size == 1) {
    479:     if (draw == *sdraw) {
    480:       PetscObjectDereference((PetscObject)draw);
    481:       *sdraw = NULL;
    482:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Cannot restore singleton, it is not the parent draw");
    483:   } else {
    484:     if (draw->ops->restoresingleton) {
    485:       (*draw->ops->restoresingleton)(draw,sdraw);
    486:     } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot restore singleton for this type %s of draw object",((PetscObject)draw)->type_name);
    487:   }
    488:   return(0);
    489: }