Actual source code: drawreg.c

petsc-dev 2014-02-02
Report Typos and Errors
  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
  5: #include <petsc-private/drawimpl.h>  /*I "petscdraw.h" I*/
  6: #include <petscviewer.h>             /*I "petscviewer.h" I*/
  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;

 18: /*@C
 19:    PetscDrawView - Prints the PetscDraw data structure.

 21:    Collective on PetscDraw

 23:    Input Parameters:
 24: +  indraw - the PetscDraw context
 25: -  viewer - visualization context

 27:    Options Database Keys:
 28: .  -draw_view - print the ksp data structure at the end of a PetscDrawSetFromOptions() call

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

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

 41:    Level: beginner

 43: .keywords: PetscDraw, view

 45: .seealso: PCView(), PetscViewerASCIIOpen()
 46: @*/
 47: PetscErrorCode  PetscDrawView(PetscDraw indraw,PetscViewer viewer)
 48: {
 50:   PetscBool      isdraw;
 51: #if defined(PETSC_HAVE_SAWS)
 52:   PetscBool      isams;
 53: #endif

 57:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject)indraw));

 61:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
 62: #if defined(PETSC_HAVE_SAWS)
 63:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&isams);
 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:     PetscDrawBoxedString(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 (isams) {
 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: }

 95: /*@C
 96:    PetscDrawCreate - Creates a graphics context.

 98:    Collective on MPI_Comm

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

108:    Output Parameter:
109: .  draw - location to put the PetscDraw context

111:    Level: beginner

113:    Concepts: graphics^creating context
114:    Concepts: drawing^creating context

116: .seealso: PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType()
117: @*/
118: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
119: {
120:   PetscDraw      draw;
122:   PetscReal      dpause;
123:   PetscBool      flag;

126:   PetscDrawInitializePackage();
127:   *indraw = 0;
128:   PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);

130:   draw->data    = 0;
131:   PetscStrallocpy(title,&draw->title);
132:   PetscStrallocpy(display,&draw->display);
133:   draw->x       = x;
134:   draw->y       = y;
135:   draw->w       = w;
136:   draw->h       = h;
137:   draw->pause   = 0.0;
138:   draw->coor_xl = 0.0;
139:   draw->coor_xr = 1.0;
140:   draw->coor_yl = 0.0;
141:   draw->coor_yr = 1.0;
142:   draw->port_xl = 0.0;
143:   draw->port_xr = 1.0;
144:   draw->port_yl = 0.0;
145:   draw->port_yr = 1.0;
146:   draw->popup   = 0;

148:   PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flag);
149:   if (flag) draw->pause = dpause;
150:   draw->savefilename  = NULL;
151:   draw->savefilemovie = PETSC_FALSE;
152:   draw->savefilecount = -1;

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

156:   draw->boundbox_xl  = .5;
157:   draw->boundbox_xr  = .5;
158:   draw->boundbox_yl  = .9;
159:   draw->boundbox_yr  = .9;

161:   *indraw = draw;
162:   return(0);
163: }

167: /*@C
168:    PetscDrawSetType - Builds graphics object for a particular implementation

170:    Collective on PetscDraw

172:    Input Parameter:
173: +  draw      - the graphics context
174: -  type      - for example, PETSC_DRAW_X

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

180:    Level: intermediate

182:    Notes:
183:    See "petsc/include/petscdraw.h" for available methods (for instance,
184:    PETSC_DRAW_X)

186:    Concepts: drawing^X windows
187:    Concepts: X windows^graphics
188:    Concepts: drawing^Microsoft Windows

190: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
191: @*/
192: PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
193: {
194:   PetscErrorCode ierr,(*r)(PetscDraw);
195:   PetscBool      match;
196:   PetscBool      flg=PETSC_FALSE;


202:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
203:   if (match) return(0);

205:   /*  User requests no graphics */
206:   PetscOptionsHasName(NULL,"-nox",&flg);

208:   /*
209:      This is not ideal, but it allows codes to continue to run if X graphics
210:    was requested but is not installed on this machine. Mostly this is for
211:    testing.
212:    */
213: #if !defined(PETSC_HAVE_X)
214:   if (!flg) {
215:     PetscStrcmp(type,PETSC_DRAW_X,&match);
216:     if (match) {
217:       PetscBool dontwarn = PETSC_TRUE;
218:       flg  = PETSC_TRUE;
219:       PetscOptionsHasName(NULL,"-nox_warning",&dontwarn);
220:       if (!dontwarn) (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
221:     }
222:   }
223: #endif
224:   if (flg) type = PETSC_DRAW_NULL;

226:   if (draw->data) {
227:     /* destroy the old private PetscDraw context */
228:     (*draw->ops->destroy)(draw);
229:     draw->ops->destroy = NULL;
230:     draw->data         = 0;
231:   }

233:    PetscFunctionListFind(PetscDrawList,type,&r);
234:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
235:   PetscObjectChangeTypeName((PetscObject)draw,type);
236:   draw->data = 0;
237:   (*r)(draw);
238:   return(0);
239: }

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

246:    Not Collective

248:    Input Parameter:
249: .  draw - Krylov context

251:    Output Parameters:
252: .  name - name of PetscDraw method

254:    Level: advanced

256: @*/
257: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
258: {
262:   *type = ((PetscObject)draw)->type_name;
263:   return(0);
264: }

268: /*@C
269:    PetscDrawRegister - Adds a method to the graphics package.

271:    Not Collective

273:    Input Parameters:
274: +  name_solver - name of a new user-defined graphics class
275: -  routine_create - routine to create method context

277:    Level: developer

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

282:    Sample usage:
283: .vb
284:    PetscDrawRegister("my_draw_type", MyDrawCreate);
285: .ve

287:    Then, your specific graphics package can be chosen with the procedural interface via
288: $     PetscDrawSetType(ksp,"my_draw_type")
289:    or at runtime via the option
290: $     -draw_type my_draw_type

292:    Concepts: graphics^registering new draw classes
293:    Concepts: PetscDraw^registering new draw classes

295: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy()
296: @*/
297: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
298: {

302:   PetscFunctionListAdd(&PetscDrawList,sname,function);
303:   return(0);
304: }

308: /*@
309:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
310:       Defaults to a PETSc X windows graphics.

312:    Collective on PetscDraw

314:    Input Parameter:
315: .     draw - the graphics context

317:    Options Database Keys:
318: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
319: .   -nox_warning - when X windows support is not installed this prevents the warning message from being printed
320: .   -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed
321: .   -draw_save [optional filename] - (X windows only) saves each image before it is cleared to a file
322: .   -draw_save_final_image [optional filename] - (X windows only) saves the final image displayed in a window
323: .   -draw_save_movie - converts image files to a movie  at the end of the run. See PetscDrawSetSave()
324: .   -draw_save_on_flush - saves an image on each flush in addition to each clear
325: -   -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename_%d

327:    Level: intermediate

329:    Notes:
330:     Must be called after PetscDrawCreate() before the PetscDraw is used.

332:     Concepts: drawing^setting options
333:     Concepts: graphics^setting options

335: .seealso: PetscDrawCreate(), PetscDrawSetType(), PetscDrawSetSave(), PetscDrawSetSaveFinalImage()

337: @*/
338: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
339: {
340:   PetscErrorCode    ierr;
341:   PetscBool         flg,nox;
342:   char              vtype[256];
343:   const char        *def;
344:   PetscReal         dpause;
345: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
346:   PetscBool         warn;
347: #endif


352:   if (!PetscDrawList) {
353:     PetscDrawRegisterAll();
354:   }

356:   if (((PetscObject)draw)->type_name) def = ((PetscObject)draw)->type_name;
357:   else {
358:     PetscOptionsHasName(NULL,"-nox",&nox);
359:     def  = PETSC_DRAW_NULL;
360: #if defined(PETSC_USE_WINDOWS_GRAPHICS)
361:     if (!nox) def = PETSC_DRAW_WIN32;
362: #elif defined(PETSC_HAVE_X)
363:     if (!nox) def = PETSC_DRAW_X;
364: #elif defined(PETSC_HAVE_GLUT)
365:     if (!nox) def = PETSC_DRAW_GLUT;
366: #elif defined(PETSC_HAVE_OPENGLES)
367:     if (!nox) def = PETSC_DRAW_OPENGLES;
368: #else
369:     PetscOptionsHasName(NULL,"-nox_warning",&warn);
370:     if (!nox && !warn) (*PetscErrorPrintf)("PETSc installed without X windows, Microsoft Graphics, OpenGL ES, or GLUT/OpenGL on this machine\nproceeding without graphics\n");
371: #endif
372:   }
373:   PetscObjectOptionsBegin((PetscObject)draw);
374:   PetscOptionsFList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
375:   if (flg) {
376:     PetscDrawSetType(draw,vtype);
377:   } else if (!((PetscObject)draw)->type_name) {
378:     PetscDrawSetType(draw,def);
379:   }
380:   PetscOptionsName("-nox","Run without graphics","None",&nox);
381: #if defined(PETSC_HAVE_X)
382:   {
383:     char      filename[PETSC_MAX_PATH_LEN];
384:     PetscBool save,movie = PETSC_FALSE;
385:     PetscOptionsBool("-draw_save_movie","Make a movie from the images saved (X Windows only)","PetscDrawSetSave",movie,&movie,NULL);
386:     PetscOptionsBool("-draw_save_single_file","Each new image replaces previous image in file","PetscDrawSetSave",draw->savesinglefile,&draw->savesinglefile,NULL);
387:     PetscOptionsString("-draw_save","Save graphics to file (X Windows only)","PetscDrawSetSave",filename,filename,PETSC_MAX_PATH_LEN,&save);
388:     if (save) {
389:       PetscDrawSetSave(draw,filename,movie);
390:     }
391:     PetscOptionsString("-draw_save_final_image","Save graphics to file (X Windows only)","PetscDrawSetSaveFinalImage",filename,filename,PETSC_MAX_PATH_LEN,&save);
392:     if (save) {
393:       PetscDrawSetSaveFinalImage(draw,filename);
394:     }
395:     PetscOptionsBool("-draw_save_on_flush","Save graphics to file (X Windows only) on each flush","PetscDrawSetSave",draw->saveonflush,&draw->saveonflush,NULL);
396:   }
397: #endif
398:   PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flg);
399:   if (flg) draw->pause = dpause;

401:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
402:   PetscObjectProcessOptionsHandlers((PetscObject)draw);

404:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
405:   PetscOptionsEnd();
406:   return(0);
407: }

411: /*@C
412:    PetscDrawSetSave - Saves images produced in a PetscDraw into a file as a Gif file using AfterImage

414:    Collective on PetscDraw

416:    Input Parameter:
417: +  draw      - the graphics context
418: .  filename  - name of the file, if NULL uses name of draw object
419: -  movie - produce a movie of all the images

421:    Options Database Command:
422: +  -draw_save  <filename>
423: -  -draw_save_movie

425:    Level: intermediate

427:    Concepts: X windows^graphics

429:    Notes: You should call this BEFORE calling PetscDrawClear() and creating your image.

431:    Requires that PETSc be configured with the option --with-afterimage to save the images and ffmpeg must be in your path to make the movie

433:    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the
434:    ./configure flags --x-includes=/pathtoXincludes --x-libraries=/pathtoXlibraries   For example under Mac OS X Mountain Lion --x-includes=/opt/X11/include -x-libraries=/opt/X11/lib


437: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage()
438: @*/
439: PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie)
440: {

445:   PetscFree(draw->savefilename);

447:   draw->savefilemovie = movie;
448:   if (filename && filename[0]) {
449:     PetscStrallocpy(filename,&draw->savefilename);
450:   } else {
451:     const char *name;
452:     PetscObjectGetName((PetscObject)draw,&name);
453:     PetscStrallocpy(name,&draw->savefilename);
454:   }
455:   if (draw->ops->setsave) {
456:     (*draw->ops->setsave)(draw,draw->savefilename);
457:   }
458:   return(0);
459: }

463: /*@C
464:    PetscDrawSetSaveFinalImage - Saves the finale image produced in a PetscDraw into a file as a Gif file using AfterImage

466:    Collective on PetscDraw

468:    Input Parameter:
469: +  draw      - the graphics context
470: -  filename  - name of the file, if NULL uses name of draw object

472:    Options Database Command:
473: .  -draw_save_final_image  <filename>

475:    Level: intermediate

477:    Concepts: X windows^graphics

479:    Notes: You should call this BEFORE calling PetscDrawClear() and creating your image.

481:    Requires that PETSc be configured with the option --with-afterimage to save the images and ffmpeg must be in your path to make the movie

483:    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the
484:    ./configure flags --x-includes=/pathtoXincludes --x-libraries=/pathtoXlibraries   For example under Mac OS X Mountain Lion --x-includes=/opt/X11/include -x-libraries=/opt/X11/lib


487: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSave()
488: @*/
489: PetscErrorCode  PetscDrawSetSaveFinalImage(PetscDraw draw,const char *filename)
490: {

495:   PetscFree(draw->savefinalfilename);

497:   if (filename && filename[0]) {
498:     PetscStrallocpy(filename,&draw->savefinalfilename);
499:   } else {
500:     const char *name;
501:     PetscObjectGetName((PetscObject)draw,&name);
502:     PetscStrallocpy(name,&draw->savefinalfilename);
503:   }
504:   return(0);
505: }