Actual source code: drawreg.c

petsc-master 2019-08-19
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: .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) {
 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:    PetscDrawCreate - Creates a graphics context.

 94:    Collective

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

104:    Output Parameter:
105: .  draw - location to put the PetscDraw context

107:    Level: beginner


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

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

130:   PetscDrawInitializePackage();
131:   *indraw = 0;
132:   PetscHeaderCreate(draw,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);

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

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

155:   draw->savefilename   = NULL;
156:   draw->saveimageext   = NULL;
157:   draw->savemovieext   = NULL;
158:   draw->savefilecount  = 0;
159:   draw->savesinglefile = PETSC_FALSE;
160:   draw->savemoviefps   = PETSC_DECIDE;

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

164:   draw->boundbox_xl  = .5;
165:   draw->boundbox_xr  = .5;
166:   draw->boundbox_yl  = .9;
167:   draw->boundbox_yr  = .9;

169:   *indraw = draw;
170:   return(0);
171: }

173: /*@C
174:    PetscDrawSetType - Builds graphics object for a particular implementation

176:    Collective on PetscDraw

178:    Input Parameter:
179: +  draw      - the graphics context
180: -  type      - for example, PETSC_DRAW_X

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

185:    See PetscDrawSetFromOptions() for additional options database keys

187:    Level: intermediate

189:    Notes:
190:    See "petsc/include/petscdraw.h" for available methods (for instance,
191:    PETSC_DRAW_X, PETSC_DRAW_TIKZ or PETSC_DRAW_IMAGE)

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


205:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
206:   if (match) return(0);

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

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

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

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

249:    Not Collective

251:    Input Parameter:
252: .  draw - Krylov context

254:    Output Parameters:
255: .  name - name of PetscDraw method

257:    Level: advanced

259: .seealso: PetscDrawSetType(), PetscDrawType

261: @*/
262: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
263: {
267:   *type = ((PetscObject)draw)->type_name;
268:   return(0);
269: }

271: /*@C
272:    PetscDrawRegister - Adds a method to the graphics package.

274:    Not Collective

276:    Input Parameters:
277: +  name_solver - name of a new user-defined graphics class
278: -  routine_create - routine to create method context

280:    Level: developer

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

285:    Sample usage:
286: .vb
287:    PetscDrawRegister("my_draw_type", MyDrawCreate);
288: .ve

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


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

303:   PetscDrawInitializePackage();
304:   PetscFunctionListAdd(&PetscDrawList,sname,function);
305:   return(0);
306: }

308: /*@C
309:    PetscDrawSetOptionsPrefix - Sets the prefix used for searching for all
310:    PetscDraw options in the database.

312:    Logically Collective on PetscDraw

314:    Input Parameter:
315: +  draw - the draw context
316: -  prefix - the prefix to prepend to all option names

318:    Level: advanced

320: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate()
321: @*/
322: PetscErrorCode  PetscDrawSetOptionsPrefix(PetscDraw draw,const char prefix[])
323: {

328:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
329:   return(0);
330: }

332: /*@
333:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
334:       Defaults to a PETSc X windows graphics.

336:    Collective on PetscDraw

338:    Input Parameter:
339: .     draw - the graphics context

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

353:    Level: intermediate

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


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

361: @*/
362: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
363: {
364:   PetscErrorCode    ierr;
365:   PetscBool         flg,nox;
366:   char              vtype[256];
367:   const char        *def;
368: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
369:   PetscBool         warn;
370: #endif


375:   PetscDrawRegisterAll();

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

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

421:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
422:   PetscOptionsEnd();
423:   return(0);
424: }