Actual source code: drawreg.c

petsc-3.11.4 2019-09-28
Report Typos and Errors

  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 = 0;

 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: .keywords: PetscDraw, view

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

 54:   if (!viewer) {
 55:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)indraw),&viewer);
 56:   }

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

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

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

 93: /*@C
 94:    PetscDrawCreate - Creates a graphics context.

 96:    Collective on MPI_Comm

 98:    Input Parameter:
 99: +  comm - MPI communicator
100: .  display - X display when using X windows
101: .  title - optional title added to top of window
102: .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
103: -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
104:           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE

106:    Output Parameter:
107: .  draw - location to put the PetscDraw context

109:    Level: beginner

111:    Concepts: graphics^creating context
112:    Concepts: drawing^creating context

114: .seealso: PetscDrawSetType(), PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType(), PetscDrawLGCreate(), PetscDrawSPCreate(),
115:           PetscDrawViewPortsCreate(), PetscDrawViewPortsSet(), PetscDrawAxisCreate(), PetscDrawHGCreate(), PetscDrawBarCreate(),
116:           PetscViewerDrawGetDraw(), PetscDrawSetFromOptions(), PetscDrawSetSave(), PetscDrawSetSaveMovie(), PetscDrawSetSaveFinalImage(),
117:           PetscDrawOpenX(), PetscDrawOpenImage(), PetscDrawIsNull(), PetscDrawGetPopup(), PetscDrawCheckResizedWindow(), PetscDrawResizeWindow(),
118:           PetscDrawGetWindowSize(), PetscDrawLine(), PetscDrawArrow(), PetscDrawLineSetWidth(), PetscDrawLineGetWidth(), PetscDrawMarker(),
119:           PetscDrawPoint(), PetscDrawRectangle(), PetscDrawTriangle(), PetscDrawEllipse(), PetscDrawString(), PetscDrawStringCentered(),
120:           PetscDrawStringBoxed(), PetscDrawStringBoxed(), PetscDrawStringVertical(), PetscDrawSetViewPort(), PetscDrawGetViewPort(),
121:           PetscDrawSplitViewPort(), PetscDrawSetTitle(), PetscDrawAppendTitle(), PetscDrawGetTitle(), PetscDrawSetPause(), PetscDrawGetPause(),
122:           PetscDrawPause(), PetscDrawSetDoubleBuffer(), PetscDrawClear(), PetscDrawFlush(), PetscDrawGetSingleton(), PetscDrawGetMouseButton(),
123:           PetscDrawZoom(), PetscDrawGetBoundingBox()

125: @*/
126: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
127: {
128:   PetscDraw      draw;
130:   PetscReal      dpause = 0.0;
131:   PetscBool      flag;

134:   PetscDrawInitializePackage();
135:   *indraw = 0;
136:   PetscHeaderCreate(draw,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);

138:   draw->data    = NULL;
139:   PetscStrallocpy(display,&draw->display);
140:   PetscStrallocpy(title,&draw->title);
141:   draw->x       = x;
142:   draw->y       = y;
143:   draw->w       = w;
144:   draw->h       = h;
145:   draw->pause   = 0.0;
146:   draw->coor_xl = 0.0;
147:   draw->coor_xr = 1.0;
148:   draw->coor_yl = 0.0;
149:   draw->coor_yr = 1.0;
150:   draw->port_xl = 0.0;
151:   draw->port_xr = 1.0;
152:   draw->port_yl = 0.0;
153:   draw->port_yr = 1.0;
154:   draw->popup   = NULL;

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

159:   draw->savefilename   = NULL;
160:   draw->saveimageext   = NULL;
161:   draw->savemovieext   = NULL;
162:   draw->savefilecount  = 0;
163:   draw->savesinglefile = PETSC_FALSE;
164:   draw->savemoviefps   = PETSC_DECIDE;

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

168:   draw->boundbox_xl  = .5;
169:   draw->boundbox_xr  = .5;
170:   draw->boundbox_yl  = .9;
171:   draw->boundbox_yr  = .9;

173:   *indraw = draw;
174:   return(0);
175: }

177: /*@C
178:    PetscDrawSetType - Builds graphics object for a particular implementation

180:    Collective on PetscDraw

182:    Input Parameter:
183: +  draw      - the graphics context
184: -  type      - for example, PETSC_DRAW_X

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

189:    See PetscDrawSetFromOptions() for additional options database keys

191:    Level: intermediate

193:    Notes:
194:    See "petsc/include/petscdraw.h" for available methods (for instance,
195:    PETSC_DRAW_X, PETSC_DRAW_TIKZ or PETSC_DRAW_IMAGE)

197:    Concepts: drawing^X windows
198:    Concepts: X windows^graphics
199:    Concepts: drawing^Microsoft Windows

201: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawType
202: @*/
203: PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
204: {
205:   PetscErrorCode ierr,(*r)(PetscDraw);
206:   PetscBool      match;
207:   PetscBool      flg=PETSC_FALSE;


213:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
214:   if (match) return(0);

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

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

245:    PetscFunctionListFind(PetscDrawList,type,&r);
246:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
247:   if (draw->ops->destroy) {(*draw->ops->destroy)(draw);}
248:   PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));
249:   PetscObjectChangeTypeName((PetscObject)draw,type);
250:   (*r)(draw);
251:   return(0);
252: }

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

257:    Not Collective

259:    Input Parameter:
260: .  draw - Krylov context

262:    Output Parameters:
263: .  name - name of PetscDraw method

265:    Level: advanced

267: .seealso: PetscDrawSetType(), PetscDrawType

269: @*/
270: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
271: {
275:   *type = ((PetscObject)draw)->type_name;
276:   return(0);
277: }

279: /*@C
280:    PetscDrawRegister - Adds a method to the graphics package.

282:    Not Collective

284:    Input Parameters:
285: +  name_solver - name of a new user-defined graphics class
286: -  routine_create - routine to create method context

288:    Level: developer

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

293:    Sample usage:
294: .vb
295:    PetscDrawRegister("my_draw_type", MyDrawCreate);
296: .ve

298:    Then, your specific graphics package can be chosen with the procedural interface via
299: $     PetscDrawSetType(ksp,"my_draw_type")
300:    or at runtime via the option
301: $     -draw_type my_draw_type

303:    Concepts: graphics^registering new draw classes
304:    Concepts: PetscDraw^registering new draw classes

306: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy(), PetscDrawType, PetscDrawSetType()
307: @*/
308: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
309: {

313:   PetscDrawInitializePackage();
314:   PetscFunctionListAdd(&PetscDrawList,sname,function);
315:   return(0);
316: }

318: /*@C
319:    PetscDrawSetOptionsPrefix - Sets the prefix used for searching for all
320:    PetscDraw options in the database.

322:    Logically Collective on PetscDraw

324:    Input Parameter:
325: +  draw - the draw context
326: -  prefix - the prefix to prepend to all option names

328:    Level: advanced

330: .keywords: PetscDraw, set, options, prefix, database

332: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate()
333: @*/
334: PetscErrorCode  PetscDrawSetOptionsPrefix(PetscDraw draw,const char prefix[])
335: {

340:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
341:   return(0);
342: }

344: /*@
345:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
346:       Defaults to a PETSc X windows graphics.

348:    Collective on PetscDraw

350:    Input Parameter:
351: .     draw - the graphics context

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

365:    Level: intermediate

367:    Notes:
368:     Must be called after PetscDrawCreate() before the PetscDraw is used.

370:    Concepts: drawing^setting options
371:    Concepts: graphics^setting options

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

375: @*/
376: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
377: {
378:   PetscErrorCode    ierr;
379:   PetscBool         flg,nox;
380:   char              vtype[256];
381:   const char        *def;
382: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
383:   PetscBool         warn;
384: #endif


389:   PetscDrawRegisterAll();

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

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

435:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
436:   PetscOptionsEnd();
437:   return(0);
438: }