Actual source code: drawreg.c

petsc-3.4.5 2014-06-29
  2: /*
  3:        Provides the registration process for PETSc PetscDraw routines
  4: */
  5: #include <petsc-private/drawimpl.h>  /*I "petscdraw.h" I*/

  7: /*
  8:    Contains the list of registered PetscDraw routines
  9: */
 10: PetscFunctionList PetscDrawList = 0;

 14: /*@C
 15:    PetscDrawCreate - Creates a graphics context.

 17:    Collective on MPI_Comm

 19:    Input Parameter:
 20: +  comm - MPI communicator
 21: .  display - X display when using X windows
 22: .  title - optional title added to top of window
 23: .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
 24: -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
 25:           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE

 27:    Output Parameter:
 28: .  draw - location to put the PetscDraw context

 30:    Level: beginner

 32:    Concepts: graphics^creating context
 33:    Concepts: drawing^creating context

 35: .seealso: PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType()
 36: @*/
 37: PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
 38: {
 39:   PetscDraw      draw;
 41:   PetscReal      dpause;
 42:   PetscBool      flag;

 45: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 46:   PetscDrawInitializePackage();
 47: #endif
 48:   *indraw = 0;
 49:   PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,0);

 51:   draw->data    = 0;
 52:   PetscStrallocpy(title,&draw->title);
 53:   PetscStrallocpy(display,&draw->display);
 54:   draw->x       = x;
 55:   draw->y       = y;
 56:   draw->w       = w;
 57:   draw->h       = h;
 58:   draw->pause   = 0.0;
 59:   draw->coor_xl = 0.0;
 60:   draw->coor_xr = 1.0;
 61:   draw->coor_yl = 0.0;
 62:   draw->coor_yr = 1.0;
 63:   draw->port_xl = 0.0;
 64:   draw->port_xr = 1.0;
 65:   draw->port_yl = 0.0;
 66:   draw->port_yr = 1.0;
 67:   draw->popup   = 0;

 69:   PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flag);
 70:   if (flag) draw->pause = dpause;
 71:   draw->savefilename  = NULL;
 72:   draw->savefilemovie = PETSC_FALSE;
 73:   draw->savefilecount = -1;

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

 77:   draw->boundbox_xl  = .5;
 78:   draw->boundbox_xr  = .5;
 79:   draw->boundbox_yl  = .9;
 80:   draw->boundbox_yr  = .9;

 82:   *indraw = draw;
 83:   return(0);
 84: }

 88: /*@C
 89:    PetscDrawSetType - Builds graphics object for a particular implementation

 91:    Collective on PetscDraw

 93:    Input Parameter:
 94: +  draw      - the graphics context
 95: -  type      - for example, PETSC_DRAW_X

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

101:    Level: intermediate

103:    Notes:
104:    See "petsc/include/petscdraw.h" for available methods (for instance,
105:    PETSC_DRAW_X)

107:    Concepts: drawing^X windows
108:    Concepts: X windows^graphics
109:    Concepts: drawing^Microsoft Windows

111: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
112: @*/
113: PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
114: {
115:   PetscErrorCode ierr,(*r)(PetscDraw);
116:   PetscBool      match;
117:   PetscBool      flg=PETSC_FALSE;


123:   PetscObjectTypeCompare((PetscObject)draw,type,&match);
124:   if (match) return(0);

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

129:   /*
130:      This is not ideal, but it allows codes to continue to run if X graphics
131:    was requested but is not installed on this machine. Mostly this is for
132:    testing.
133:    */
134: #if !defined(PETSC_HAVE_X)
135:   if (!flg) {
136:     PetscStrcmp(type,PETSC_DRAW_X,&match);
137:     if (match) {
138:       PetscBool dontwarn = PETSC_TRUE;
139:       flg  = PETSC_TRUE;
140:       PetscOptionsHasName(NULL,"-nox_warning",&dontwarn);
141:       if (!dontwarn) (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
142:     }
143:   }
144: #endif
145:   if (flg) type = PETSC_DRAW_NULL;

147:   if (draw->data) {
148:     /* destroy the old private PetscDraw context */
149:     (*draw->ops->destroy)(draw);
150:     draw->ops->destroy = NULL;
151:     draw->data         = 0;
152:   }

154:    PetscFunctionListFind(PetscDrawList,type,&r);
155:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
156:   PetscObjectChangeTypeName((PetscObject)draw,type);
157:   draw->data = 0;
158:   (*r)(draw);
159:   return(0);
160: }

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

167:    Not Collective

169:    Input Parameter:
170: .  draw - Krylov context

172:    Output Parameters:
173: .  name - name of PetscDraw method

175:    Level: advanced

177: @*/
178: PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
179: {
183:   *type = ((PetscObject)draw)->type_name;
184:   return(0);
185: }

189: /*@C
190:    PetscDrawRegister - Adds a method to the graphics package.

192:    Not Collective

194:    Input Parameters:
195: +  name_solver - name of a new user-defined solver
196: -  routine_create - routine to create method context

198:    Level: developer

200:    Notes:
201:    PetscDrawRegister() may be called multiple times to add several user-defined solvers.

203:    Sample usage:
204: .vb
205:    PetscDrawRegister("my_draw_type", MyDrawCreate);
206: .ve

208:    Then, your solver can be chosen with the procedural interface via
209: $     PetscDrawSetType(ksp,"my_draw_type")
210:    or at runtime via the option
211: $     -draw_type my_draw_type

213:    Concepts: graphics^registering new draw classes
214:    Concepts: PetscDraw^registering new draw classes

216: .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy()
217: @*/
218: PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
219: {

223:   PetscFunctionListAdd(&PetscDrawList,sname,function);
224:   return(0);
225: }

229: /*@
230:    PetscDrawSetFromOptions - Sets the graphics type from the options database.
231:       Defaults to a PETSc X windows graphics.

233:    Collective on PetscDraw

235:    Input Parameter:
236: .     draw - the graphics context

238:    Options Database Keys:
239: +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
240: .   -nox_warning - when X windows support is not installed this prevents the warning message
241:                    from being printed
242: .   -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed
243: .   -draw_save [optional filename] - (X windows only) saves each image before it is cleared to a file
244: .   -draw_save_movie - converts image files to a movie  at the end of the run. See PetscDrawSetSave()
245: -   -draw_save_on_flush - saves an image on each flush in addition to each clear

247:    Level: intermediate

249:    Notes:
250:     Must be called after PetscDrawCreate() before the PetscDrawtor is used.

252:     Concepts: drawing^setting options
253:     Concepts: graphics^setting options

255: .seealso: PetscDrawCreate(), PetscDrawSetType(), PetscDrawSetSave()

257: @*/
258: PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
259: {
261:   PetscBool      flg,nox;
262:   char           vtype[256];
263:   const char     *def;
264:   PetscReal      dpause;
265: #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
266:   PetscBool      warn;
267: #endif


272:   if (!PetscDrawList) {
273:     PetscDrawRegisterAll();
274:   }

276:   if (((PetscObject)draw)->type_name) def = ((PetscObject)draw)->type_name;
277:   else {
278:     PetscOptionsHasName(NULL,"-nox",&nox);
279:     def  = PETSC_DRAW_NULL;
280: #if defined(PETSC_USE_WINDOWS_GRAPHICS)
281:     if (!nox) def = PETSC_DRAW_WIN32;
282: #elif defined(PETSC_HAVE_X)
283:     if (!nox) def = PETSC_DRAW_X;
284: #elif defined(PETSC_HAVE_GLUT)
285:     if (!nox) def = PETSC_DRAW_GLUT;
286: #elif defined(PETSC_HAVE_OPENGLES)
287:     if (!nox) def = PETSC_DRAW_OPENGLES;
288: #else
289:     PetscOptionsHasName(NULL,"-nox_warning",&warn);
290:     if (!nox && !warn) (*PetscErrorPrintf)("PETSc installed without X windows, Microsoft Graphics, OpenGL ES, or GLUT/OpenGL on this machine\nproceeding without graphics\n");
291: #endif
292:   }
293:   PetscObjectOptionsBegin((PetscObject)draw);
294:   PetscOptionsList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);
295:   if (flg) {
296:     PetscDrawSetType(draw,vtype);
297:   } else if (!((PetscObject)draw)->type_name) {
298:     PetscDrawSetType(draw,def);
299:   }
300:   PetscOptionsName("-nox","Run without graphics","None",&nox);
301: #if defined(PETSC_HAVE_X)
302:   {
303:     char      filename[PETSC_MAX_PATH_LEN];
304:     PetscBool save,movie = PETSC_FALSE;
305:     PetscOptionsBool("-draw_save_movie","Make a movie from the images saved (X Windows only)","PetscDrawSetSave",movie,&movie,NULL);
306:     PetscOptionsString("-draw_save","Save graphics to file (X Windows only)","PetscDrawSetSave",filename,filename,PETSC_MAX_PATH_LEN,&save);
307:     if (save) {
308:       PetscDrawSetSave(draw,filename,movie);
309:     }
310:     PetscOptionsBool("-draw_save_on_flush","Save graphics to file (X Windows only) on each flush","PetscDrawSetSave",draw->saveonflush,&draw->saveonflush,NULL);
311:   }
312: #endif
313:   PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flg);
314:   if (flg) draw->pause = dpause;

316:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
317:   PetscObjectProcessOptionsHandlers((PetscObject)draw);
318:   PetscOptionsEnd();
319:   return(0);
320: }

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

327:    Collective on PetscDraw

329:    Input Parameter:
330: +  draw      - the graphics context
331: .  filename  - name of the file, if NULL uses name of draw object
332: -  movie - produce a movie of all the images

334:    Options Database Command:
335: +  -draw_save  <filename>
336: -  -draw_save_movie

338:    Level: intermediate

340:    Concepts: X windows^graphics

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

344:    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

346:    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the
347:    ./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


350: .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
351: @*/
352: PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie)
353: {

358:   PetscFree(draw->savefilename);

360:   draw->savefilemovie = movie;
361:   if (filename && filename[0]) {
362:     PetscStrallocpy(filename,&draw->savefilename);
363:   } else {
364:     const char *name;
365:     PetscObjectGetName((PetscObject)draw,&name);
366:     PetscStrallocpy(name,&draw->savefilename);
367:   }
368:   if (draw->ops->setsave) {
369:     (*draw->ops->setsave)(draw,filename);
370:   }
371:   return(0);
372: }