Date: 2022-08-02T03:13:17+00:00

Actual source code: drawreg.c

      2: /*
      3:        Provides the registration process for PETSc PetscDraw routines
      4: */
      5: #include <petsc/private/drawimpl.h>
      6: #include <petscviewer.h>
      7: #if defined(PETSC_HAVE_SAWS)
      8: #include <petscviewersaws.h>
      9: #endif

     11: /*
     12:    Contains the list of registered PetscDraw routines
     13: */
     14: PetscFunctionList PetscDrawList = NULL;

     16: /*@C
     17:    PetscDrawView - Prints the PetscDraw data structure.

     19:    Collective on PetscDraw

     21:    Input Parameters:
     22: +  indraw - the PetscDraw context
     23: -  viewer - visualization context

     25:    See PetscDrawSetFromOptions() for options database keys

     27:    Note:
     28:    The available visualization contexts include
     29: +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
     30: -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
     31:          output where only the first processor opens
     32:          the file.  All other processors send their
     33:          data to the first processor to print.

     35:    The user can open an alternative visualization context with
     36:    PetscViewerASCIIOpen() - output to a specified file.

     38:    Level: beginner

     40: .seealso: PCView(), PetscViewerASCIIOpen()
     41: @*/
     42: PetscErrorCode  PetscDrawView(PetscDraw indraw,PetscViewer viewer)
     43: {
     44:   PetscBool      isdraw;
     45: #if defined(PETSC_HAVE_SAWS)
     46:   PetscBool      issaws;
     47: #endif

     50:   if (!viewer) {
     51:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)indraw),&viewer);
     52:   }

     56:   PetscObjectPrintClassNamePrefixType((PetscObject)indraw,viewer);
     57:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
     58: #if defined(PETSC_HAVE_SAWS)
     59:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
     60: #endif
     61:   if (isdraw) {
     62:     PetscDraw draw;
     63:     char      str[36];
     64:     PetscReal x,y,bottom,h;

     66:     PetscViewerDrawGetDraw(viewer,0,&draw);
     67:     PetscDrawGetCurrentPoint(draw,&x,&y);
     68:     PetscStrncpy(str,"PetscDraw: ",sizeof(str));
     69:     PetscStrlcat(str,((PetscObject)indraw)->type_name,sizeof(str));
     70:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
     71:     bottom = y - h;
     72:     PetscDrawPushCurrentPoint(draw,x,bottom);
     73: #if defined(PETSC_HAVE_SAWS)
     74:   } else if (issaws) {
     75:     PetscMPIInt rank;

     77:     PetscObjectName((PetscObject)indraw);
     78:     MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     79:     if (!((PetscObject)indraw)->amsmem && rank == 0) {
     80:       PetscObjectViewSAWs((PetscObject)indraw,viewer);
     81:     }
     82: #endif
     83:   } else if (indraw->ops->view) {
     84:     (*indraw->ops->view)(indraw,viewer);
     85:   }
     86:   return 0;
     87: }

     89: /*@C
     90:    PetscDrawViewFromOptions - View from Options

     92:    Collective on PetscDraw

     94:    Input Parameters:
     95: +  A - the PetscDraw context
     96: .  obj - Optional object
     97: -  name - command line option

     99:    Level: intermediate
    100: .seealso:  PetscDraw, PetscDrawView, PetscObjectViewFromOptions(), PetscDrawCreate()
    101: @*/
    102: PetscErrorCode  PetscDrawViewFromOptions(PetscDraw A,PetscObject obj,const char name[])
    103: {
    105:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
    106:   return 0;
    107: }

    109: /*@C
    110:    PetscDrawCreate - Creates a graphics context.

    112:    Collective

    114:    Input Parameters:
    115: +  comm - MPI communicator
    116: .  display - X display when using X Windows
    117: .  title - optional title added to top of window
    118: .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
    119: -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
    120:           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE

    122:    Output Parameter:
    123: .  draw - location to put the PetscDraw context

    125:    Level: beginner

    127: .seealso: PetscDrawSetType(), PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType(), PetscDrawLGCreate(), PetscDrawSPCreate(),
    128:           PetscDrawViewPortsCreate(), PetscDrawViewPortsSet(), PetscDrawAxisCreate(), PetscDrawHGCreate(), PetscDrawBarCreate(),
    129:           PetscViewerDrawGetDraw(), PetscDrawSetFromOptions(), PetscDrawSetSave(), PetscDrawSetSaveMovie(), PetscDrawSetSaveFinalImage(),
    130:           PetscDrawOpenX(), PetscDrawOpenImage(), PetscDrawIsNull(), PetscDrawGetPopup(), PetscDrawCheckResizedWindow(), PetscDrawResizeWindow(),
    131:           PetscDrawGetWindowSize(), PetscDrawLine(), PetscDrawArrow(), PetscDrawLineSetWidth(), PetscDrawLineGetWidth(), PetscDrawMarker(),
    132:           PetscDrawPoint(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(), PetscDrawString(), PetscDrawStringCentered(),
    133:           PetscDrawStringBoxed(), PetscDrawStringVertical(), PetscDrawSetViewPort(), PetscDrawGetViewPort(),
    134:           PetscDrawSplitViewPort(), PetscDrawSetTitle(), PetscDrawAppendTitle(), PetscDrawGetTitle(), PetscDrawSetPause(), PetscDrawGetPause(),
    135:           PetscDrawPause(), PetscDrawSetDoubleBuffer(), PetscDrawClear(), PetscDrawFlush(), PetscDrawGetSingleton(), PetscDrawGetMouseButton(),
    136:           PetscDrawZoom(), PetscDrawGetBoundingBox()

    138: @*/
    139: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
    140: {
    141:   PetscDraw      draw;
    142:   PetscReal      dpause = 0.0;
    143:   PetscBool      flag;

    145:   PetscDrawInitializePackage();
    146:   *indraw = NULL;
    147:   PetscHeaderCreate(draw,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);

    149:   draw->data    = NULL;
    150:   PetscStrallocpy(display,&draw->display);
    151:   PetscStrallocpy(title,&draw->title);
    152:   draw->x       = x;
    153:   draw->y       = y;
    154:   draw->w       = w;
    155:   draw->h       = h;
    156:   draw->pause   = 0.0;
    157:   draw->coor_xl = 0.0;
    158:   draw->coor_xr = 1.0;
    159:   draw->coor_yl = 0.0;
    160:   draw->coor_yr = 1.0;
    161:   draw->port_xl = 0.0;
    162:   draw->port_xr = 1.0;
    163:   draw->port_yl = 0.0;
    164:   draw->port_yr = 1.0;
    165:   draw->popup   = NULL;

    167:   PetscOptionsGetReal(NULL,NULL,"-draw_pause",&dpause,&flag);
    168:   if (flag) draw->pause = dpause;

    170:   draw->savefilename   = NULL;
    171:   draw->saveimageext   = NULL;
    172:   draw->savemovieext   = NULL;
    173:   draw->savefilecount  = 0;
    174:   draw->savesinglefile = PETSC_FALSE;
    175:   draw->savemoviefps   = PETSC_DECIDE;

    177:   PetscDrawSetCurrentPoint(draw,.5,.9);

    179:   draw->boundbox_xl  = .5;
    180:   draw->boundbox_xr  = .5;
    181:   draw->boundbox_yl  = .9;
    182:   draw->boundbox_yr  = .9;

    184:   *indraw = draw;
    185:   return 0;
    186: }

    188: /*@C
    189:    PetscDrawSetType - Builds graphics object for a particular implementation

    191:    Collective on PetscDraw

    193:    Input Parameters:
    194: +  draw      - the graphics context
    195: -  type      - for example, PETSC_DRAW_X

    197:    Options Database Command:
    198: .  -draw_type  <type> - Sets the type; use -help for a list of available methods (for instance, x)

    200:    See PetscDrawSetFromOptions() for additional options database keys

    202:    Level: intermediate

    204:    Notes:
    205:    See "petsc/include/petscdraw.h" for available methods (for instance,
    206:    PETSC_DRAW_X, PETSC_DRAW_TIKZ or PETSC_DRAW_IMAGE)

    208: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawType
    209: @*/
    210: PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
    211: {
    212:   PetscBool      match;
    213:   PetscBool      flg=PETSC_FALSE;
    214:   PetscErrorCode (*r)(PetscDraw);

    219:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
    220:   if (match) return 0;

    222:   /*  User requests no graphics */
    223:   PetscOptionsHasName(((PetscObject)draw)->options,NULL,"-nox",&flg);

    225:   /*
    226:      This is not ideal, but it allows codes to continue to run if X graphics
    227:    was requested but is not installed on this machine. Mostly this is for
    228:    testing.
    229:    */
    230: #if !defined(PETSC_HAVE_X)
    231:   if (!flg) {
    232:     PetscStrcmp(type,PETSC_DRAW_X,&match);
    233:     if (match) {
    234:       PetscBool dontwarn = PETSC_TRUE;
    235:       flg  = PETSC_TRUE;
    236:       PetscOptionsHasName(NULL,NULL,"-nox_warning",&dontwarn);
    237:       if (!dontwarn) (*PetscErrorPrintf)("PETSc installed without X Windows on this machine\nproceeding without graphics\n");
    238:     }
    239:   }
    240: #endif
    241:   if (flg) {
    242:     PetscStrcmp(type,"tikz",&flg);
    243:     if (!flg) type = PETSC_DRAW_NULL;
    244:   }

    246:   PetscStrcmp(type,PETSC_DRAW_NULL,&match);
    247:   if (match) {
    248:     PetscOptionsHasName(NULL,NULL,"-draw_double_buffer",NULL);
    249:     PetscOptionsHasName(NULL,NULL,"-draw_virtual",NULL);
    250:     PetscOptionsHasName(NULL,NULL,"-draw_fast",NULL);
    251:     PetscOptionsHasName(NULL,NULL,"-draw_ports",NULL);
    252:     PetscOptionsHasName(NULL,NULL,"-draw_coordinates",NULL);
    253:   }

    255:   PetscFunctionListFind(PetscDrawList,type,&r);
    257:   if (draw->ops->destroy) (*draw->ops->destroy)(draw);
    258:   PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));
    259:   PetscObjectChangeTypeName((PetscObject)draw,type);
    260:   (*r)(draw);
    261:   return 0;
    262: }

    264: /*@C
    265:    PetscDrawGetType - Gets the PetscDraw type as a string from the PetscDraw object.

    267:    Not Collective

    269:    Input Parameter:
    270: .  draw - Krylov context

    272:    Output Parameters:
    273: .  name - name of PetscDraw method

    275:    Level: advanced

    277: .seealso: PetscDrawSetType(), PetscDrawType

    279: @*/
    280: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
    281: {
    284:   *type = ((PetscObject)draw)->type_name;
    285:   return 0;
    286: }

    288: /*@C
    289:    PetscDrawRegister - Adds a method to the graphics package.

    291:    Not Collective

    293:    Input Parameters:
    294: +  name_solver - name of a new user-defined graphics class
    295: -  routine_create - routine to create method context

    297:    Level: developer

    299:    Notes:
    300:    PetscDrawRegister() may be called multiple times to add several user-defined graphics classes

    302:    Sample usage:
    303: .vb
    304:    PetscDrawRegister("my_draw_type", MyDrawCreate);
    305: .ve

    307:    Then, your specific graphics package can be chosen with the procedural interface via
    308: $     PetscDrawSetType(ksp,"my_draw_type")
    309:    or at runtime via the option
    310: $     -draw_type my_draw_type

    312: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy(), PetscDrawType, PetscDrawSetType()
    313: @*/
    314: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
    315: {
    316:   PetscDrawInitializePackage();
    317:   PetscFunctionListAdd(&PetscDrawList,sname,function);
    318:   return 0;
    319: }

    321: /*@C
    322:    PetscDrawSetOptionsPrefix - Sets the prefix used for searching for all
    323:    PetscDraw options in the database.

    325:    Logically Collective on PetscDraw

    327:    Input Parameters:
    328: +  draw - the draw context
    329: -  prefix - the prefix to prepend to all option names

    331:    Level: advanced

    333: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate()
    334: @*/
    335: PetscErrorCode  PetscDrawSetOptionsPrefix(PetscDraw draw,const char prefix[])
    336: {
    338:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
    339:   return 0;
    340: }

    342: /*@
    343:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
    344:       Defaults to a PETSc X Windows graphics.

    346:    Collective on PetscDraw

    348:    Input Parameter:
    349: .     draw - the graphics context

    351:    Options Database Keys:
    352: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
    353: .   -nox_warning - when X Windows support is not installed this prevents the warning message from being printed
    354: .   -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed
    355: .   -draw_marker_type - <x,point>
    356: .   -draw_save [optional filename] - (X Windows only) saves each image before it is cleared to a file
    357: .   -draw_save_final_image [optional filename] - (X Windows only) saves the final image displayed in a window
    358: .   -draw_save_movie - converts image files to a movie  at the end of the run. See PetscDrawSetSave()
    359: .   -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with 'filename/filename_%d.ext'
    360: .   -draw_save_on_clear - saves an image on each clear, mainly for debugging
    361: -   -draw_save_on_flush - saves an image on each flush, mainly for debugging

    363:    Level: intermediate

    365:    Notes:
    366:     Must be called after PetscDrawCreate() before the PetscDraw is used.

    368: .seealso: PetscDrawCreate(), PetscDrawSetType(), PetscDrawSetSave(), PetscDrawSetSaveFinalImage(), PetscDrawPause(), PetscDrawSetPause()

    370: @*/
    371: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
    372: {
    373:   PetscErrorCode    ierr;
    374:   PetscBool         flg,nox;
    375:   char              vtype[256];
    376:   const char        *def;
    377: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
    378:   PetscBool         warn;
    379: #endif

    383:   PetscDrawRegisterAll();

    385:   if (((PetscObject)draw)->type_name) def = ((PetscObject)draw)->type_name;
    386:   else {
    387:     PetscOptionsHasName(((PetscObject)draw)->options,NULL,"-nox",&nox);
    388:     def  = PETSC_DRAW_NULL;
    389: #if defined(PETSC_USE_WINDOWS_GRAPHICS)
    390:     if (!nox) def = PETSC_DRAW_WIN32;
    391: #elif defined(PETSC_HAVE_X)
    392:     if (!nox) def = PETSC_DRAW_X;
    393: #else
    394:     PetscOptionsHasName(NULL,NULL,"-nox_warning",&warn);
    395:     if (!nox && !warn) (*PetscErrorPrintf)("PETSc installed without X Windows or Microsoft Graphics on this machine\nproceeding without graphics\n");
    396: #endif
    397:   }
    398:   PetscObjectOptionsBegin((PetscObject)draw);
    399:   PetscOptionsFList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
    400:   if (flg) {
    401:     PetscDrawSetType(draw,vtype);
    402:   } else if (!((PetscObject)draw)->type_name) {
    403:     PetscDrawSetType(draw,def);
    404:   }
    405:   PetscOptionsName("-nox","Run without graphics","None",&nox);
    406:   {
    407:     char      filename[PETSC_MAX_PATH_LEN];
    408:     char      movieext[32];
    409:     PetscBool image,movie;
    410:     PetscSNPrintf(filename,sizeof(filename),"%s%s",draw->savefilename?draw->savefilename:"",draw->saveimageext?draw->saveimageext:"");
    411:     PetscSNPrintf(movieext,sizeof(movieext),"%s",draw->savemovieext?draw->savemovieext:"");
    412:     PetscOptionsString("-draw_save","Save graphics to image file","PetscDrawSetSave",filename,filename,sizeof(filename),&image);
    413:     PetscOptionsString("-draw_save_movie","Make a movie from saved images","PetscDrawSetSaveMovie",movieext,movieext,sizeof(movieext),&movie);
    414:     PetscOptionsInt("-draw_save_movie_fps","Set frames per second in saved movie",PETSC_FUNCTION_NAME,draw->savemoviefps,&draw->savemoviefps,NULL);
    415:     PetscOptionsBool("-draw_save_single_file","Each new image replaces previous image in file",PETSC_FUNCTION_NAME,draw->savesinglefile,&draw->savesinglefile,NULL);
    416:     if (image) PetscDrawSetSave(draw,filename);
    417:     if (movie) PetscDrawSetSaveMovie(draw,movieext);
    418:     PetscOptionsString("-draw_save_final_image","Save final graphics to image file","PetscDrawSetSaveFinalImage",filename,filename,sizeof(filename),&image);
    419:     if (image) PetscDrawSetSaveFinalImage(draw,filename);
    420:     PetscOptionsBool("-draw_save_on_clear","Save graphics to file on each clear",PETSC_FUNCTION_NAME,draw->saveonclear,&draw->saveonclear,NULL);
    421:     PetscOptionsBool("-draw_save_on_flush","Save graphics to file on each flush",PETSC_FUNCTION_NAME,draw->saveonflush,&draw->saveonflush,NULL);
    422:   }
    423:   PetscOptionsReal("-draw_pause","Amount of time that program pauses after plots","PetscDrawSetPause",draw->pause,&draw->pause,NULL);
    424:   PetscOptionsEnum("-draw_marker_type","Type of marker to use on plots","PetscDrawSetMarkerType",PetscDrawMarkerTypes,(PetscEnum)draw->markertype,(PetscEnum *)&draw->markertype,NULL);

    426:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
    427:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)draw);

    429:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
    430:   PetscOptionsEnd();
    431:   return 0;
    432: }