Actual source code: drawreg.c

petsc-3.13.6 2020-09-29
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 = 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) {
 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 Parameter:
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


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

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

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

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

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

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

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

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

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

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

199:    Collective on PetscDraw

201:    Input Parameter:
202: +  draw      - the graphics context
203: -  type      - for example, PETSC_DRAW_X

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

208:    See PetscDrawSetFromOptions() for additional options database keys

210:    Level: intermediate

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

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


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

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

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

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

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

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

276:    Not Collective

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

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

284:    Level: advanced

286: .seealso: PetscDrawSetType(), PetscDrawType

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

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

301:    Not Collective

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

307:    Level: developer

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

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

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


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

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

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

339:    Logically Collective on PetscDraw

341:    Input Parameter:
342: +  draw - the draw context
343: -  prefix - the prefix to prepend to all option names

345:    Level: advanced

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

355:   PetscObjectSetOptionsPrefix((PetscObject)draw,prefix);
356:   return(0);
357: }

359: /*@
360:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
361:       Defaults to a PETSc X windows graphics.

363:    Collective on PetscDraw

365:    Input Parameter:
366: .     draw - the graphics context

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

380:    Level: intermediate

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


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

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


402:   PetscDrawRegisterAll();

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

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

448:   PetscDrawViewFromOptions(draw,NULL,"-draw_view");
449:   PetscOptionsEnd();
450:   return(0);
451: }