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: {
 45:   PetscBool      isdraw;
 46: #if defined(PETSC_HAVE_SAWS)
 47:   PetscBool      issaws;
 48: #endif

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

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

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

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

 91: /*@C
 92:    PetscDrawViewFromOptions - View from Options

 94:    Collective on PetscDraw

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

101:    Level: intermediate
102: .seealso:  PetscDraw, PetscDrawView, PetscObjectViewFromOptions(), PetscDrawCreate()
103: @*/
104: PetscErrorCode  PetscDrawViewFromOptions(PetscDraw A,PetscObject obj,const char name[])
105: {

110:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
111:   return(0);
112: }

114: /*@C
115:    PetscDrawCreate - Creates a graphics context.

117:    Collective

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

127:    Output Parameter:
128: .  draw - location to put the PetscDraw context

130:    Level: beginner

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

143: @*/
144: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
145: {
146:   PetscDraw      draw;
148:   PetscReal      dpause = 0.0;
149:   PetscBool      flag;

152:   PetscDrawInitializePackage();
153:   *indraw = NULL;
154:   PetscHeaderCreate(draw,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);

156:   draw->data    = NULL;
157:   PetscStrallocpy(display,&draw->display);
158:   PetscStrallocpy(title,&draw->title);
159:   draw->x       = x;
160:   draw->y       = y;
161:   draw->w       = w;
162:   draw->h       = h;
163:   draw->pause   = 0.0;
164:   draw->coor_xl = 0.0;
165:   draw->coor_xr = 1.0;
166:   draw->coor_yl = 0.0;
167:   draw->coor_yr = 1.0;
168:   draw->port_xl = 0.0;
169:   draw->port_xr = 1.0;
170:   draw->port_yl = 0.0;
171:   draw->port_yr = 1.0;
172:   draw->popup   = NULL;

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

177:   draw->savefilename   = NULL;
178:   draw->saveimageext   = NULL;
179:   draw->savemovieext   = NULL;
180:   draw->savefilecount  = 0;
181:   draw->savesinglefile = PETSC_FALSE;
182:   draw->savemoviefps   = PETSC_DECIDE;

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

186:   draw->boundbox_xl  = .5;
187:   draw->boundbox_xr  = .5;
188:   draw->boundbox_yl  = .9;
189:   draw->boundbox_yr  = .9;

191:   *indraw = draw;
192:   return(0);
193: }

195: /*@C
196:    PetscDrawSetType - Builds graphics object for a particular implementation

198:    Collective on PetscDraw

200:    Input Parameters:
201: +  draw      - the graphics context
202: -  type      - for example, PETSC_DRAW_X

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

207:    See PetscDrawSetFromOptions() for additional options database keys

209:    Level: intermediate

211:    Notes:
212:    See "petsc/include/petscdraw.h" for available methods (for instance,
213:    PETSC_DRAW_X, PETSC_DRAW_TIKZ or PETSC_DRAW_IMAGE)

215: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawType
216: @*/
217: PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
218: {
219:   PetscErrorCode ierr,(*r)(PetscDraw);
220:   PetscBool      match;
221:   PetscBool      flg=PETSC_FALSE;


227:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
228:   if (match) return(0);

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

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

254:   PetscStrcmp(type,PETSC_DRAW_NULL,&match);
255:   if (match) {
256:     PetscOptionsHasName(NULL,NULL,"-draw_double_buffer",NULL);
257:     PetscOptionsHasName(NULL,NULL,"-draw_virtual",NULL);
258:     PetscOptionsHasName(NULL,NULL,"-draw_fast",NULL);
259:     PetscOptionsHasName(NULL,NULL,"-draw_ports",NULL);
260:     PetscOptionsHasName(NULL,NULL,"-draw_coordinates",NULL);
261:   }

263:    PetscFunctionListFind(PetscDrawList,type,&r);
264:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
265:   if (draw->ops->destroy) {(*draw->ops->destroy)(draw);}
266:   PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));
267:   PetscObjectChangeTypeName((PetscObject)draw,type);
268:   (*r)(draw);
269:   return(0);
270: }

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

275:    Not Collective

277:    Input Parameter:
278: .  draw - Krylov context

280:    Output Parameters:
281: .  name - name of PetscDraw method

283:    Level: advanced

285: .seealso: PetscDrawSetType(), PetscDrawType

287: @*/
288: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
289: {
293:   *type = ((PetscObject)draw)->type_name;
294:   return(0);
295: }

297: /*@C
298:    PetscDrawRegister - Adds a method to the graphics package.

300:    Not Collective

302:    Input Parameters:
303: +  name_solver - name of a new user-defined graphics class
304: -  routine_create - routine to create method context

306:    Level: developer

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

311:    Sample usage:
312: .vb
313:    PetscDrawRegister("my_draw_type", MyDrawCreate);
314: .ve

316:    Then, your specific graphics package can be chosen with the procedural interface via
317: $     PetscDrawSetType(ksp,"my_draw_type")
318:    or at runtime via the option
319: $     -draw_type my_draw_type

321: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy(), PetscDrawType, PetscDrawSetType()
322: @*/
323: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
324: {

328:   PetscDrawInitializePackage();
329:   PetscFunctionListAdd(&PetscDrawList,sname,function);
330:   return(0);
331: }

333: /*@C
334:    PetscDrawSetOptionsPrefix - Sets the prefix used for searching for all
335:    PetscDraw options in the database.

337:    Logically Collective on PetscDraw

339:    Input Parameters:
340: +  draw - the draw context
341: -  prefix - the prefix to prepend to all option names

343:    Level: advanced

345: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate()
346: @*/
347: PetscErrorCode  PetscDrawSetOptionsPrefix(PetscDraw draw,const char prefix[])
348: {

353:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
354:   return(0);
355: }

357: /*@
358:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
359:       Defaults to a PETSc X Windows graphics.

361:    Collective on PetscDraw

363:    Input Parameter:
364: .     draw - the graphics context

366:    Options Database Keys:
367: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
368: .   -nox_warning - when X Windows support is not installed this prevents the warning message from being printed
369: .   -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed
370: .   -draw_marker_type - <x,point>
371: .   -draw_save [optional filename] - (X Windows only) saves each image before it is cleared to a file
372: .   -draw_save_final_image [optional filename] - (X Windows only) saves the final image displayed in a window
373: .   -draw_save_movie - converts image files to a movie  at the end of the run. See PetscDrawSetSave()
374: .   -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'
375: .   -draw_save_on_clear - saves an image on each clear, mainly for debugging
376: -   -draw_save_on_flush - saves an image on each flush, mainly for debugging

378:    Level: intermediate

380:    Notes:
381:     Must be called after PetscDrawCreate() before the PetscDraw is used.

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

385: @*/
386: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
387: {
388:   PetscErrorCode    ierr;
389:   PetscBool         flg,nox;
390:   char              vtype[256];
391:   const char        *def;
392: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
393:   PetscBool         warn;
394: #endif


399:   PetscDrawRegisterAll();

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

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

445:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
446:   PetscOptionsEnd();
447:   return(0);
448: }