Actual source code: drawreg.c

petsc-3.8.0 2017-09-26
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:     PetscStrcmp(type,"tikz",&flg);
237:     if (!flg) type = PETSC_DRAW_NULL;
238:   }

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

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

252:    Not Collective

254:    Input Parameter:
255: .  draw - Krylov context

257:    Output Parameters:
258: .  name - name of PetscDraw method

260:    Level: advanced

262: .seealso: PetscDrawSetType(), PetscDrawType

264: @*/
265: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
266: {
270:   *type = ((PetscObject)draw)->type_name;
271:   return(0);
272: }

274: /*@C
275:    PetscDrawRegister - Adds a method to the graphics package.

277:    Not Collective

279:    Input Parameters:
280: +  name_solver - name of a new user-defined graphics class
281: -  routine_create - routine to create method context

283:    Level: developer

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

288:    Sample usage:
289: .vb
290:    PetscDrawRegister("my_draw_type", MyDrawCreate);
291: .ve

293:    Then, your specific graphics package can be chosen with the procedural interface via
294: $     PetscDrawSetType(ksp,"my_draw_type")
295:    or at runtime via the option
296: $     -draw_type my_draw_type

298:    Concepts: graphics^registering new draw classes
299:    Concepts: PetscDraw^registering new draw classes

301: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy(), PetscDrawType, PetscDrawSetType()
302: @*/
303: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
304: {

308:   PetscFunctionListAdd(&PetscDrawList,sname,function);
309:   return(0);
310: }

312: /*@C
313:    PetscDrawSetOptionsPrefix - Sets the prefix used for searching for all
314:    PetscDraw options in the database.

316:    Logically Collective on PetscDraw

318:    Input Parameter:
319: +  draw - the draw context
320: -  prefix - the prefix to prepend to all option names

322:    Level: advanced

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

326: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate()
327: @*/
328: PetscErrorCode  PetscDrawSetOptionsPrefix(PetscDraw draw,const char prefix[])
329: {

334:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
335:   return(0);
336: }

338: /*@
339:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
340:       Defaults to a PETSc X windows graphics.

342:    Collective on PetscDraw

344:    Input Parameter:
345: .     draw - the graphics context

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

359:    Level: intermediate

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

363:    Concepts: drawing^setting options
364:    Concepts: graphics^setting options

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

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


382:   PetscDrawRegisterAll();

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

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

432:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
433:   PetscOptionsEnd();
434:   return(0);
435: }