Actual source code: drawreg.c

petsc-3.8.2 2017-11-09
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:     PetscStrcpy(str,"PetscDraw: ");
 73:     PetscStrcat(str,((PetscObject)indraw)->type_name);
 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(), PetscDrawStringBoxedSize(), 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:     PetscStrcmp(type,"tikz",&flg);
241:     if (!flg) type = PETSC_DRAW_NULL;
242:   }

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

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

256:    Not Collective

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

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

264:    Level: advanced

266: .seealso: PetscDrawSetType(), PetscDrawType

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

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

281:    Not Collective

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

287:    Level: developer

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

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

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

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

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

312:   PetscFunctionListAdd(&PetscDrawList,sname,function);
313:   return(0);
314: }

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

320:    Logically Collective on PetscDraw

322:    Input Parameter:
323: +  draw - the draw context
324: -  prefix - the prefix to prepend to all option names

326:    Level: advanced

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

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

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: Must be called after PetscDrawCreate() before the PetscDraw is used.

367:    Concepts: drawing^setting options
368:    Concepts: graphics^setting options

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

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


386:   PetscDrawRegisterAll();

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

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

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