1 2 /* 3 Provides the registration process for PETSc PetscDraw routines 4 */ 5 #include <petsc-private/drawimpl.h> /*I "petscdraw.h" I*/ 6 7 /* 8 Contains the list of registered PetscDraw routines 9 */ 10 PetscFunctionList PetscDrawList = 0; 11 12 #undef __FUNCT__ 13 #define __FUNCT__ "PetscDrawCreate" 14 /*@C 15 PetscDrawCreate - Creates a graphics context. 16 17 Collective on MPI_Comm 18 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 26 27 Output Parameter: 28 . draw - location to put the PetscDraw context 29 30 Level: beginner 31 32 Concepts: graphics^creating context 33 Concepts: drawing^creating context 34 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; 40 PetscErrorCode ierr; 41 PetscReal dpause; 42 PetscBool flag; 43 44 PetscFunctionBegin; 45 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 46 ierr = PetscDrawInitializePackage(NULL);CHKERRQ(ierr); 47 #endif 48 *indraw = 0; 49 ierr = PetscHeaderCreate(draw,_p_PetscDraw,struct _PetscDrawOps,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,0);CHKERRQ(ierr); 50 51 draw->data = 0; 52 ierr = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr); 53 ierr = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr); 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; 68 69 ierr = PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flag);CHKERRQ(ierr); 70 if (flag) draw->pause = dpause; 71 draw->savefilename = NULL; 72 draw->savefilemovie = PETSC_FALSE; 73 draw->savefilecount = -1; 74 75 ierr = PetscDrawSetCurrentPoint(draw,.5,.9);CHKERRQ(ierr); 76 77 draw->boundbox_xl = .5; 78 draw->boundbox_xr = .5; 79 draw->boundbox_yl = .9; 80 draw->boundbox_yr = .9; 81 82 *indraw = draw; 83 PetscFunctionReturn(0); 84 } 85 86 #undef __FUNCT__ 87 #define __FUNCT__ "PetscDrawSetType" 88 /*@C 89 PetscDrawSetType - Builds graphics object for a particular implementation 90 91 Collective on PetscDraw 92 93 Input Parameter: 94 + draw - the graphics context 95 - type - for example, PETSC_DRAW_X 96 97 Options Database Command: 98 . -draw_type <type> - Sets the type; use -help for a list 99 of available methods (for instance, x) 100 101 Level: intermediate 102 103 Notes: 104 See "petsc/include/petscdraw.h" for available methods (for instance, 105 PETSC_DRAW_X) 106 107 Concepts: drawing^X windows 108 Concepts: X windows^graphics 109 Concepts: drawing^Microsoft Windows 110 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; 118 119 PetscFunctionBegin; 120 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 121 PetscValidCharPointer(type,2); 122 123 ierr = PetscObjectTypeCompare((PetscObject)draw,type,&match);CHKERRQ(ierr); 124 if (match) PetscFunctionReturn(0); 125 126 /* User requests no graphics */ 127 ierr = PetscOptionsHasName(NULL,"-nox",&flg);CHKERRQ(ierr); 128 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 ierr = PetscStrcmp(type,PETSC_DRAW_X,&match);CHKERRQ(ierr); 137 if (match) { 138 PetscBool dontwarn = PETSC_TRUE; 139 flg = PETSC_TRUE; 140 ierr = PetscOptionsHasName(NULL,"-nox_warning",&dontwarn);CHKERRQ(ierr); 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; 146 147 if (draw->data) { 148 /* destroy the old private PetscDraw context */ 149 ierr = (*draw->ops->destroy)(draw);CHKERRQ(ierr); 150 draw->ops->destroy = NULL; 151 draw->data = 0; 152 } 153 154 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)draw),PetscDrawList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 155 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type); 156 ierr = PetscObjectChangeTypeName((PetscObject)draw,type);CHKERRQ(ierr); 157 draw->data = 0; 158 ierr = (*r)(draw);CHKERRQ(ierr); 159 PetscFunctionReturn(0); 160 } 161 162 #undef __FUNCT__ 163 #define __FUNCT__ "PetscDrawRegisterDestroy" 164 /*@C 165 PetscDrawRegisterDestroy - Frees the list of PetscDraw methods that were 166 registered by PetscDrawRegister(). 167 168 Not Collective 169 170 Level: developer 171 172 .seealso: PetscDrawRegister(), PetscDrawRegisterAll() 173 @*/ 174 PetscErrorCode PetscDrawRegisterDestroy(void) 175 { 176 PetscErrorCode ierr; 177 178 PetscFunctionBegin; 179 ierr = PetscFunctionListDestroy(&PetscDrawList);CHKERRQ(ierr); 180 PetscFunctionReturn(0); 181 } 182 183 #undef __FUNCT__ 184 #define __FUNCT__ "PetscDrawGetType" 185 /*@C 186 PetscDrawGetType - Gets the PetscDraw type as a string from the PetscDraw object. 187 188 Not Collective 189 190 Input Parameter: 191 . draw - Krylov context 192 193 Output Parameters: 194 . name - name of PetscDraw method 195 196 Level: advanced 197 198 @*/ 199 PetscErrorCode PetscDrawGetType(PetscDraw draw,PetscDrawType *type) 200 { 201 PetscFunctionBegin; 202 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 203 PetscValidPointer(type,2); 204 *type = ((PetscObject)draw)->type_name; 205 PetscFunctionReturn(0); 206 } 207 208 #undef __FUNCT__ 209 #define __FUNCT__ "PetscDrawRegister" 210 /*@ 211 PetscDrawRegister - Adds a method to the graphics package. 212 213 Not Collective 214 215 Input Parameters: 216 + name_solver - name of a new user-defined solver 217 . name_create - name of routine to create method context 218 - routine_create - routine to create method context 219 220 Level: developer 221 222 Notes: 223 PetscDrawRegister() may be called multiple times to add several user-defined solvers. 224 225 Sample usage: 226 .vb 227 PetscDrawRegister("my_draw_type", "MyDrawCreate",MyDrawCreate); 228 .ve 229 230 Then, your solver can be chosen with the procedural interface via 231 $ PetscDrawSetType(ksp,"my_draw_type") 232 or at runtime via the option 233 $ -draw_type my_draw_type 234 235 Concepts: graphics^registering new draw classes 236 Concepts: PetscDraw^registering new draw classes 237 238 .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy() 239 @*/ 240 PetscErrorCode PetscDrawRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscDraw)) 241 { 242 PetscErrorCode ierr; 243 char fullname[PETSC_MAX_PATH_LEN]; 244 245 PetscFunctionBegin; 246 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 247 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscDrawList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 248 PetscFunctionReturn(0); 249 } 250 251 #undef __FUNCT__ 252 #define __FUNCT__ "PetscDrawSetFromOptions" 253 /*@ 254 PetscDrawSetFromOptions - Sets the graphics type from the options database. 255 Defaults to a PETSc X windows graphics. 256 257 Collective on PetscDraw 258 259 Input Parameter: 260 . draw - the graphics context 261 262 Options Database Keys: 263 + -nox - do not use X graphics (ignore graphics calls, but run program correctly) 264 . -nox_warning - when X windows support is not installed this prevents the warning message 265 from being printed 266 . -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed 267 . -draw_save [optional filename] - (X windows only) saves each image before it is cleared to a file 268 . -draw_save_movie - converts image files to a movie at the end of the run. See PetscDrawSetSave() 269 - -draw_save_on_flush - saves an image on each flush in addition to each clear 270 271 Level: intermediate 272 273 Notes: 274 Must be called after PetscDrawCreate() before the PetscDrawtor is used. 275 276 Concepts: drawing^setting options 277 Concepts: graphics^setting options 278 279 .seealso: PetscDrawCreate(), PetscDrawSetType(), PetscDrawSetSave() 280 281 @*/ 282 PetscErrorCode PetscDrawSetFromOptions(PetscDraw draw) 283 { 284 PetscErrorCode ierr; 285 PetscBool flg,nox; 286 char vtype[256]; 287 const char *def; 288 PetscReal dpause; 289 #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X) 290 PetscBool warn; 291 #endif 292 293 PetscFunctionBegin; 294 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 295 296 if (!PetscDrawList) { 297 ierr = PetscDrawRegisterAll(NULL);CHKERRQ(ierr); 298 } 299 300 if (((PetscObject)draw)->type_name) def = ((PetscObject)draw)->type_name; 301 else { 302 ierr = PetscOptionsHasName(NULL,"-nox",&nox);CHKERRQ(ierr); 303 def = PETSC_DRAW_NULL; 304 #if defined(PETSC_USE_WINDOWS_GRAPHICS) 305 if (!nox) def = PETSC_DRAW_WIN32; 306 #elif defined(PETSC_HAVE_X) 307 if (!nox) def = PETSC_DRAW_X; 308 #elif defined(PETSC_HAVE_GLUT) 309 if (!nox) def = PETSC_DRAW_GLUT; 310 #elif defined(PETSC_HAVE_OPENGLES) 311 if (!nox) def = PETSC_DRAW_OPENGLES; 312 #else 313 ierr = PetscOptionsHasName(NULL,"-nox_warning",&warn);CHKERRQ(ierr); 314 if (!nox && !warn) (*PetscErrorPrintf)("PETSc installed without X windows, Microsoft Graphics, OpenGL ES, or GLUT/OpenGL on this machine\nproceeding without graphics\n"); 315 #endif 316 } 317 ierr = PetscObjectOptionsBegin((PetscObject)draw);CHKERRQ(ierr); 318 ierr = PetscOptionsList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);CHKERRQ(ierr); 319 if (flg) { 320 ierr = PetscDrawSetType(draw,vtype);CHKERRQ(ierr); 321 } else if (!((PetscObject)draw)->type_name) { 322 ierr = PetscDrawSetType(draw,def);CHKERRQ(ierr); 323 } 324 ierr = PetscOptionsName("-nox","Run without graphics","None",&nox);CHKERRQ(ierr); 325 #if defined(PETSC_HAVE_X) 326 { 327 char filename[PETSC_MAX_PATH_LEN]; 328 PetscBool save,movie = PETSC_FALSE; 329 ierr = PetscOptionsBool("-draw_save_movie","Make a movie from the images saved (X Windows only)","PetscDrawSetSave",movie,&movie,NULL);CHKERRQ(ierr); 330 ierr = PetscOptionsString("-draw_save","Save graphics to file (X Windows only)","PetscDrawSetSave",filename,filename,PETSC_MAX_PATH_LEN,&save);CHKERRQ(ierr); 331 if (save) { 332 ierr = PetscDrawSetSave(draw,filename,movie);CHKERRQ(ierr); 333 } 334 ierr = PetscOptionsBool("-draw_save_on_flush","Save graphics to file (X Windows only) on each flush","PetscDrawSetSave",draw->saveonflush,&draw->saveonflush,NULL);CHKERRQ(ierr); 335 } 336 #endif 337 ierr = PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flg);CHKERRQ(ierr); 338 if (flg) draw->pause = dpause; 339 340 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 341 ierr = PetscObjectProcessOptionsHandlers((PetscObject)draw);CHKERRQ(ierr); 342 ierr = PetscOptionsEnd();CHKERRQ(ierr); 343 PetscFunctionReturn(0); 344 } 345 346 #undef __FUNCT__ 347 #define __FUNCT__ "PetscDrawSetSave" 348 /*@C 349 PetscDrawSetSave - Saves images produced in a PetscDraw into a file as a Gif file using AfterImage 350 351 Collective on PetscDraw 352 353 Input Parameter: 354 + draw - the graphics context 355 . filename - name of the file, if NULL uses name of draw object 356 - movie - produce a movie of all the images 357 358 Options Database Command: 359 + -draw_save <filename> 360 - -draw_save_movie 361 362 Level: intermediate 363 364 Concepts: X windows^graphics 365 366 Notes: You should call this BEFORE calling PetscDrawClear() and creating your image. 367 368 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 369 370 If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the 371 ./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 372 373 374 .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy() 375 @*/ 376 PetscErrorCode PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie) 377 { 378 PetscErrorCode ierr; 379 380 PetscFunctionBegin; 381 PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1); 382 ierr = PetscFree(draw->savefilename);CHKERRQ(ierr); 383 384 draw->savefilemovie = movie; 385 if (filename && filename[0]) { 386 ierr = PetscStrallocpy(filename,&draw->savefilename);CHKERRQ(ierr); 387 } else { 388 const char *name; 389 ierr = PetscObjectGetName((PetscObject)draw,&name);CHKERRQ(ierr); 390 ierr = PetscStrallocpy(name,&draw->savefilename);CHKERRQ(ierr); 391 } 392 if (draw->ops->setsave) { 393 ierr = (*draw->ops->setsave)(draw,filename);CHKERRQ(ierr); 394 } 395 PetscFunctionReturn(0); 396 } 397