xref: /petsc/src/sys/classes/draw/interface/drawreg.c (revision f55353a2a7c46e5e81f955495d9d64b6042b9555) !
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith /*
35c6c1daeSBarry Smith        Provides the registration process for PETSc PetscDraw routines
45c6c1daeSBarry Smith */
5af0996ceSBarry Smith #include <petsc/private/drawimpl.h>  /*I "petscdraw.h" I*/
6111ee811SBarry Smith #include <petscviewer.h>             /*I "petscviewer.h" I*/
7111ee811SBarry Smith #if defined(PETSC_HAVE_SAWS)
8111ee811SBarry Smith #include <petscviewersaws.h>
9111ee811SBarry Smith #endif
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith /*
125c6c1daeSBarry Smith    Contains the list of registered PetscDraw routines
135c6c1daeSBarry Smith */
14140e18c1SBarry Smith PetscFunctionList PetscDrawList = 0;
155c6c1daeSBarry Smith 
160076e027SBarry Smith #undef __FUNCT__
170076e027SBarry Smith #define __FUNCT__ "PetscDrawView"
180076e027SBarry Smith /*@C
190076e027SBarry Smith    PetscDrawView - Prints the PetscDraw data structure.
200076e027SBarry Smith 
210076e027SBarry Smith    Collective on PetscDraw
220076e027SBarry Smith 
230076e027SBarry Smith    Input Parameters:
240076e027SBarry Smith +  indraw - the PetscDraw context
250076e027SBarry Smith -  viewer - visualization context
260076e027SBarry Smith 
27236f5a4dSBarry Smith    See PetscDrawSetFromOptions() for options database keys
280076e027SBarry Smith 
290076e027SBarry Smith    Note:
300076e027SBarry Smith    The available visualization contexts include
310076e027SBarry Smith +     PETSC_VIEWER_STDOUT_SELF - standard output (default)
320076e027SBarry Smith -     PETSC_VIEWER_STDOUT_WORLD - synchronized standard
330076e027SBarry Smith          output where only the first processor opens
340076e027SBarry Smith          the file.  All other processors send their
350076e027SBarry Smith          data to the first processor to print.
360076e027SBarry Smith 
370076e027SBarry Smith    The user can open an alternative visualization context with
380076e027SBarry Smith    PetscViewerASCIIOpen() - output to a specified file.
390076e027SBarry Smith 
400076e027SBarry Smith    Level: beginner
410076e027SBarry Smith 
420076e027SBarry Smith .keywords: PetscDraw, view
430076e027SBarry Smith 
440076e027SBarry Smith .seealso: PCView(), PetscViewerASCIIOpen()
450076e027SBarry Smith @*/
460076e027SBarry Smith PetscErrorCode  PetscDrawView(PetscDraw indraw,PetscViewer viewer)
470076e027SBarry Smith {
480076e027SBarry Smith   PetscErrorCode ierr;
490076e027SBarry Smith   PetscBool      isdraw;
50e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
51536b137fSBarry Smith   PetscBool      issaws;
520076e027SBarry Smith #endif
530076e027SBarry Smith 
540076e027SBarry Smith   PetscFunctionBegin;
550076e027SBarry Smith   PetscValidHeaderSpecific(indraw,PETSC_DRAW_CLASSID,1);
56*f55353a2SBarry Smith   if (!viewer) {
57*f55353a2SBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)indraw),&viewer);CHKERRQ(ierr);
58*f55353a2SBarry Smith   }
590076e027SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
600076e027SBarry Smith   PetscCheckSameComm(indraw,1,viewer,2);
610076e027SBarry Smith 
6298c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)indraw,viewer);CHKERRQ(ierr);
630076e027SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);CHKERRQ(ierr);
64e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
65536b137fSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
660076e027SBarry Smith #endif
670076e027SBarry Smith   if (isdraw) {
680076e027SBarry Smith     PetscDraw draw;
690076e027SBarry Smith     char      str[36];
700076e027SBarry Smith     PetscReal x,y,bottom,h;
710076e027SBarry Smith 
720076e027SBarry Smith     ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
730076e027SBarry Smith     ierr = PetscDrawGetCurrentPoint(draw,&x,&y);CHKERRQ(ierr);
740076e027SBarry Smith     ierr   = PetscStrcpy(str,"PetscDraw: ");CHKERRQ(ierr);
750076e027SBarry Smith     ierr   = PetscStrcat(str,((PetscObject)indraw)->type_name);CHKERRQ(ierr);
7651fa3d41SBarry Smith     ierr   = PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);CHKERRQ(ierr);
770076e027SBarry Smith     bottom = y - h;
780076e027SBarry Smith     ierr = PetscDrawPushCurrentPoint(draw,x,bottom);CHKERRQ(ierr);
79e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
80536b137fSBarry Smith   } else if (issaws) {
81d45a07a7SBarry Smith     PetscMPIInt rank;
82d45a07a7SBarry Smith 
83d45a07a7SBarry Smith     ierr = PetscObjectName((PetscObject)indraw);CHKERRQ(ierr);
84d45a07a7SBarry Smith     ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
85d45a07a7SBarry Smith     if (!((PetscObject)indraw)->amsmem && !rank) {
86e04113cfSBarry Smith       ierr = PetscObjectViewSAWs((PetscObject)indraw,viewer);CHKERRQ(ierr);
870076e027SBarry Smith     }
880076e027SBarry Smith #endif
890076e027SBarry Smith   } else if (indraw->ops->view) {
900076e027SBarry Smith     ierr = (*indraw->ops->view)(indraw,viewer);CHKERRQ(ierr);
910076e027SBarry Smith   }
920076e027SBarry Smith   PetscFunctionReturn(0);
930076e027SBarry Smith }
940076e027SBarry Smith 
955c6c1daeSBarry Smith #undef __FUNCT__
965c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawCreate"
975c6c1daeSBarry Smith /*@C
985c6c1daeSBarry Smith    PetscDrawCreate - Creates a graphics context.
995c6c1daeSBarry Smith 
1005c6c1daeSBarry Smith    Collective on MPI_Comm
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith    Input Parameter:
1035c6c1daeSBarry Smith +  comm - MPI communicator
1045c6c1daeSBarry Smith .  display - X display when using X windows
1055c6c1daeSBarry Smith .  title - optional title added to top of window
1065c6c1daeSBarry Smith .  x,y - coordinates of lower left corner of window or PETSC_DECIDE
1075c6c1daeSBarry Smith -  w, h - width and height of window or PETSC_DECIDE or PETSC_DRAW_HALF_SIZE, PETSC_DRAW_FULL_SIZE,
1085c6c1daeSBarry Smith           or PETSC_DRAW_THIRD_SIZE or PETSC_DRAW_QUARTER_SIZE
1095c6c1daeSBarry Smith 
1105c6c1daeSBarry Smith    Output Parameter:
1115c6c1daeSBarry Smith .  draw - location to put the PetscDraw context
1125c6c1daeSBarry Smith 
1135c6c1daeSBarry Smith    Level: beginner
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith    Concepts: graphics^creating context
1165c6c1daeSBarry Smith    Concepts: drawing^creating context
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith .seealso: PetscDrawSetFromOptions(), PetscDrawDestroy(), PetscDrawSetType()
1195c6c1daeSBarry Smith @*/
1205c6c1daeSBarry Smith PetscErrorCode  PetscDrawCreate(MPI_Comm comm,const char display[],const char title[],int x,int y,int w,int h,PetscDraw *indraw)
1215c6c1daeSBarry Smith {
1225c6c1daeSBarry Smith   PetscDraw      draw;
1235c6c1daeSBarry Smith   PetscErrorCode ierr;
1245c6c1daeSBarry Smith   PetscReal      dpause;
1255c6c1daeSBarry Smith   PetscBool      flag;
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith   PetscFunctionBegin;
128607a6623SBarry Smith   ierr = PetscDrawInitializePackage();CHKERRQ(ierr);
1295c6c1daeSBarry Smith   *indraw = 0;
13073107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(draw,PETSC_DRAW_CLASSID,"Draw","Graphics","Draw",comm,PetscDrawDestroy,PetscDrawView);CHKERRQ(ierr);
131a297a907SKarl Rupp 
1325c6c1daeSBarry Smith   draw->data    = 0;
1335c6c1daeSBarry Smith   ierr          = PetscStrallocpy(title,&draw->title);CHKERRQ(ierr);
1345c6c1daeSBarry Smith   ierr          = PetscStrallocpy(display,&draw->display);CHKERRQ(ierr);
1355c6c1daeSBarry Smith   draw->x       = x;
1365c6c1daeSBarry Smith   draw->y       = y;
1375c6c1daeSBarry Smith   draw->w       = w;
1385c6c1daeSBarry Smith   draw->h       = h;
1395c6c1daeSBarry Smith   draw->pause   = 0.0;
1405c6c1daeSBarry Smith   draw->coor_xl = 0.0;
1415c6c1daeSBarry Smith   draw->coor_xr = 1.0;
1425c6c1daeSBarry Smith   draw->coor_yl = 0.0;
1435c6c1daeSBarry Smith   draw->coor_yr = 1.0;
1445c6c1daeSBarry Smith   draw->port_xl = 0.0;
1455c6c1daeSBarry Smith   draw->port_xr = 1.0;
1465c6c1daeSBarry Smith   draw->port_yl = 0.0;
1475c6c1daeSBarry Smith   draw->port_yr = 1.0;
1485c6c1daeSBarry Smith   draw->popup   = 0;
149a297a907SKarl Rupp 
1500298fd71SBarry Smith   ierr = PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flag);CHKERRQ(ierr);
1515c6c1daeSBarry Smith   if (flag) draw->pause = dpause;
1520298fd71SBarry Smith   draw->savefilename  = NULL;
1535c6c1daeSBarry Smith   draw->savefilemovie = PETSC_FALSE;
1545c6c1daeSBarry Smith   draw->savefilecount = -1;
155a297a907SKarl Rupp 
1565c6c1daeSBarry Smith   ierr = PetscDrawSetCurrentPoint(draw,.5,.9);CHKERRQ(ierr);
157a297a907SKarl Rupp 
1585c6c1daeSBarry Smith   draw->boundbox_xl  = .5;
1595c6c1daeSBarry Smith   draw->boundbox_xr  = .5;
1605c6c1daeSBarry Smith   draw->boundbox_yl  = .9;
1615c6c1daeSBarry Smith   draw->boundbox_yr  = .9;
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith   *indraw = draw;
1645c6c1daeSBarry Smith   PetscFunctionReturn(0);
1655c6c1daeSBarry Smith }
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith #undef __FUNCT__
1685c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawSetType"
1695c6c1daeSBarry Smith /*@C
1705c6c1daeSBarry Smith    PetscDrawSetType - Builds graphics object for a particular implementation
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith    Collective on PetscDraw
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith    Input Parameter:
1755c6c1daeSBarry Smith +  draw      - the graphics context
1765c6c1daeSBarry Smith -  type      - for example, PETSC_DRAW_X
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith    Options Database Command:
179236f5a4dSBarry Smith .  -draw_type  <type> - Sets the type; use -help for a list of available methods (for instance, x)
180236f5a4dSBarry Smith 
181236f5a4dSBarry Smith    See PetscDrawSetFromOptions for additional options database keys
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith    Level: intermediate
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith    Notes:
1865c6c1daeSBarry Smith    See "petsc/include/petscdraw.h" for available methods (for instance,
1875c6c1daeSBarry Smith    PETSC_DRAW_X)
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith    Concepts: drawing^X windows
1905c6c1daeSBarry Smith    Concepts: X windows^graphics
1915c6c1daeSBarry Smith    Concepts: drawing^Microsoft Windows
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy()
1945c6c1daeSBarry Smith @*/
1955c6c1daeSBarry Smith PetscErrorCode  PetscDrawSetType(PetscDraw draw,PetscDrawType type)
1965c6c1daeSBarry Smith {
1975c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscDraw);
1985c6c1daeSBarry Smith   PetscBool      match;
1995c6c1daeSBarry Smith   PetscBool      flg=PETSC_FALSE;
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith   PetscFunctionBegin;
2025c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
2035c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)draw,type,&match);CHKERRQ(ierr);
2065c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith   /*  User requests no graphics */
2090298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-nox",&flg);CHKERRQ(ierr);
2105c6c1daeSBarry Smith 
2115c6c1daeSBarry Smith   /*
2125c6c1daeSBarry Smith      This is not ideal, but it allows codes to continue to run if X graphics
2135c6c1daeSBarry Smith    was requested but is not installed on this machine. Mostly this is for
2145c6c1daeSBarry Smith    testing.
2155c6c1daeSBarry Smith    */
2165c6c1daeSBarry Smith #if !defined(PETSC_HAVE_X)
2175c6c1daeSBarry Smith   if (!flg) {
2185c6c1daeSBarry Smith     ierr = PetscStrcmp(type,PETSC_DRAW_X,&match);CHKERRQ(ierr);
2195c6c1daeSBarry Smith     if (match) {
2205c6c1daeSBarry Smith       PetscBool dontwarn = PETSC_TRUE;
2215c6c1daeSBarry Smith       flg  = PETSC_TRUE;
2220298fd71SBarry Smith       ierr = PetscOptionsHasName(NULL,"-nox_warning",&dontwarn);CHKERRQ(ierr);
223a297a907SKarl Rupp       if (!dontwarn) (*PetscErrorPrintf)("PETSc installed without X windows on this machine\nproceeding without graphics\n");
2245c6c1daeSBarry Smith     }
2255c6c1daeSBarry Smith   }
2265c6c1daeSBarry Smith #endif
227a297a907SKarl Rupp   if (flg) type = PETSC_DRAW_NULL;
2285c6c1daeSBarry Smith 
2291c9cd337SJed Brown   ierr =  PetscFunctionListFind(PetscDrawList,type,&r);CHKERRQ(ierr);
2305c6c1daeSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscDraw type given: %s",type);
231618c1b54SLisandro Dalcin   if (draw->ops->destroy) {ierr = (*draw->ops->destroy)(draw);CHKERRQ(ierr);}
232618c1b54SLisandro Dalcin   ierr = PetscMemzero(draw->ops,sizeof(struct _PetscDrawOps));CHKERRQ(ierr);
2335c6c1daeSBarry Smith   ierr       = PetscObjectChangeTypeName((PetscObject)draw,type);CHKERRQ(ierr);
2345c6c1daeSBarry Smith   ierr       = (*r)(draw);CHKERRQ(ierr);
2355c6c1daeSBarry Smith   PetscFunctionReturn(0);
2365c6c1daeSBarry Smith }
2375c6c1daeSBarry Smith 
2385c6c1daeSBarry Smith #undef __FUNCT__
2395c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawGetType"
2405c6c1daeSBarry Smith /*@C
2415c6c1daeSBarry Smith    PetscDrawGetType - Gets the PetscDraw type as a string from the PetscDraw object.
2425c6c1daeSBarry Smith 
2435c6c1daeSBarry Smith    Not Collective
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith    Input Parameter:
2465c6c1daeSBarry Smith .  draw - Krylov context
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith    Output Parameters:
2495c6c1daeSBarry Smith .  name - name of PetscDraw method
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith    Level: advanced
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith @*/
2545c6c1daeSBarry Smith PetscErrorCode  PetscDrawGetType(PetscDraw draw,PetscDrawType *type)
2555c6c1daeSBarry Smith {
2565c6c1daeSBarry Smith   PetscFunctionBegin;
2575c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
2585c6c1daeSBarry Smith   PetscValidPointer(type,2);
2595c6c1daeSBarry Smith   *type = ((PetscObject)draw)->type_name;
2605c6c1daeSBarry Smith   PetscFunctionReturn(0);
2615c6c1daeSBarry Smith }
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith #undef __FUNCT__
2645c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawRegister"
265607a6623SBarry Smith /*@C
2661c84c290SBarry Smith    PetscDrawRegister - Adds a method to the graphics package.
2671c84c290SBarry Smith 
2681c84c290SBarry Smith    Not Collective
2691c84c290SBarry Smith 
2701c84c290SBarry Smith    Input Parameters:
2710076e027SBarry Smith +  name_solver - name of a new user-defined graphics class
2721c84c290SBarry Smith -  routine_create - routine to create method context
2731c84c290SBarry Smith 
2741c84c290SBarry Smith    Level: developer
2751c84c290SBarry Smith 
2761c84c290SBarry Smith    Notes:
2770076e027SBarry Smith    PetscDrawRegister() may be called multiple times to add several user-defined graphics classes
2781c84c290SBarry Smith 
2791c84c290SBarry Smith    Sample usage:
2801c84c290SBarry Smith .vb
281bdf89e91SBarry Smith    PetscDrawRegister("my_draw_type", MyDrawCreate);
2821c84c290SBarry Smith .ve
2831c84c290SBarry Smith 
2840076e027SBarry Smith    Then, your specific graphics package can be chosen with the procedural interface via
2851c84c290SBarry Smith $     PetscDrawSetType(ksp,"my_draw_type")
2861c84c290SBarry Smith    or at runtime via the option
2871c84c290SBarry Smith $     -draw_type my_draw_type
2881c84c290SBarry Smith 
2891c84c290SBarry Smith    Concepts: graphics^registering new draw classes
2901c84c290SBarry Smith    Concepts: PetscDraw^registering new draw classes
2911c84c290SBarry Smith 
2921c84c290SBarry Smith .seealso: PetscDrawRegisterAll(), PetscDrawRegisterDestroy()
2931c84c290SBarry Smith @*/
294bdf89e91SBarry Smith PetscErrorCode  PetscDrawRegister(const char *sname,PetscErrorCode (*function)(PetscDraw))
2955c6c1daeSBarry Smith {
2965c6c1daeSBarry Smith   PetscErrorCode ierr;
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith   PetscFunctionBegin;
299a240a19fSJed Brown   ierr = PetscFunctionListAdd(&PetscDrawList,sname,function);CHKERRQ(ierr);
3005c6c1daeSBarry Smith   PetscFunctionReturn(0);
3015c6c1daeSBarry Smith }
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith #undef __FUNCT__
3045c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawSetFromOptions"
3055c6c1daeSBarry Smith /*@
3065c6c1daeSBarry Smith    PetscDrawSetFromOptions - Sets the graphics type from the options database.
3075c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith    Collective on PetscDraw
3105c6c1daeSBarry Smith 
3115c6c1daeSBarry Smith    Input Parameter:
3125c6c1daeSBarry Smith .     draw - the graphics context
3135c6c1daeSBarry Smith 
3145c6c1daeSBarry Smith    Options Database Keys:
3155c6c1daeSBarry Smith +   -nox - do not use X graphics (ignore graphics calls, but run program correctly)
3161cda70a7SBarry Smith .   -nox_warning - when X windows support is not installed this prevents the warning message from being printed
3177450148dSBarry Smith .   -draw_pause <pause amount> -- -1 indicates wait for mouse input, -2 indicates pause when window is to be destroyed
31873f7a4c5SBarry Smith .   -draw_marker_type - <x,point>
3195c6c1daeSBarry Smith .   -draw_save [optional filename] - (X windows only) saves each image before it is cleared to a file
320287de1a7SBarry Smith .   -draw_save_final_image [optional filename] - (X windows only) saves the final image displayed in a window
321a4494fc1SBarry Smith .   -draw_save_movie - converts image files to a movie  at the end of the run. See PetscDrawSetSave()
3228b7fcac6SBarry Smith .   -draw_save_on_flush - saves an image on each flush in addition to each clear
3238b7fcac6SBarry Smith -   -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename_%d
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith    Level: intermediate
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith    Notes:
32899749f0dSBarry Smith     Must be called after PetscDrawCreate() before the PetscDraw is used.
3295c6c1daeSBarry Smith 
3305c6c1daeSBarry Smith     Concepts: drawing^setting options
3315c6c1daeSBarry Smith     Concepts: graphics^setting options
3325c6c1daeSBarry Smith 
333287de1a7SBarry Smith .seealso: PetscDrawCreate(), PetscDrawSetType(), PetscDrawSetSave(), PetscDrawSetSaveFinalImage()
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith @*/
3365c6c1daeSBarry Smith PetscErrorCode  PetscDrawSetFromOptions(PetscDraw draw)
3375c6c1daeSBarry Smith {
3385c6c1daeSBarry Smith   PetscErrorCode    ierr;
3395c6c1daeSBarry Smith   PetscBool         flg,nox;
3405c6c1daeSBarry Smith   char              vtype[256];
3415c6c1daeSBarry Smith   const char        *def;
3427450148dSBarry Smith   PetscReal         dpause;
3435c6c1daeSBarry Smith #if !defined(PETSC_USE_WINDOWS_GRAPHICS) && !defined(PETSC_HAVE_X)
3445c6c1daeSBarry Smith   PetscBool         warn;
3455c6c1daeSBarry Smith #endif
3465c6c1daeSBarry Smith 
3475c6c1daeSBarry Smith   PetscFunctionBegin;
3485c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
3495c6c1daeSBarry Smith 
3505c6c1daeSBarry Smith   if (!PetscDrawList) {
351607a6623SBarry Smith     ierr = PetscDrawRegisterAll();CHKERRQ(ierr);
3525c6c1daeSBarry Smith   }
3535c6c1daeSBarry Smith 
354a297a907SKarl Rupp   if (((PetscObject)draw)->type_name) def = ((PetscObject)draw)->type_name;
355a297a907SKarl Rupp   else {
3560298fd71SBarry Smith     ierr = PetscOptionsHasName(NULL,"-nox",&nox);CHKERRQ(ierr);
3575c6c1daeSBarry Smith     def  = PETSC_DRAW_NULL;
3585c6c1daeSBarry Smith #if defined(PETSC_USE_WINDOWS_GRAPHICS)
3595c6c1daeSBarry Smith     if (!nox) def = PETSC_DRAW_WIN32;
3605c6c1daeSBarry Smith #elif defined(PETSC_HAVE_X)
3615c6c1daeSBarry Smith     if (!nox) def = PETSC_DRAW_X;
3625c6c1daeSBarry Smith #elif defined(PETSC_HAVE_GLUT)
3635c6c1daeSBarry Smith     if (!nox) def = PETSC_DRAW_GLUT;
3645c6c1daeSBarry Smith #elif defined(PETSC_HAVE_OPENGLES)
3655c6c1daeSBarry Smith     if (!nox) def = PETSC_DRAW_OPENGLES;
3665c6c1daeSBarry Smith #else
3670298fd71SBarry Smith     ierr = PetscOptionsHasName(NULL,"-nox_warning",&warn);CHKERRQ(ierr);
368a297a907SKarl Rupp     if (!nox && !warn) (*PetscErrorPrintf)("PETSc installed without X windows, Microsoft Graphics, OpenGL ES, or GLUT/OpenGL on this machine\nproceeding without graphics\n");
3695c6c1daeSBarry Smith #endif
3705c6c1daeSBarry Smith   }
3715c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)draw);CHKERRQ(ierr);
372a264d7a6SBarry Smith   ierr = PetscOptionsFList("-draw_type","Type of graphical output","PetscDrawSetType",PetscDrawList,def,vtype,256,&flg);CHKERRQ(ierr);
3735c6c1daeSBarry Smith   if (flg) {
3745c6c1daeSBarry Smith     ierr = PetscDrawSetType(draw,vtype);CHKERRQ(ierr);
3755c6c1daeSBarry Smith   } else if (!((PetscObject)draw)->type_name) {
3765c6c1daeSBarry Smith     ierr = PetscDrawSetType(draw,def);CHKERRQ(ierr);
3775c6c1daeSBarry Smith   }
3785c6c1daeSBarry Smith   ierr = PetscOptionsName("-nox","Run without graphics","None",&nox);CHKERRQ(ierr);
3795c6c1daeSBarry Smith #if defined(PETSC_HAVE_X)
3805c6c1daeSBarry Smith   {
3815c6c1daeSBarry Smith     char      filename[PETSC_MAX_PATH_LEN];
3825c6c1daeSBarry Smith     PetscBool save,movie = PETSC_FALSE;
3830298fd71SBarry Smith     ierr = PetscOptionsBool("-draw_save_movie","Make a movie from the images saved (X Windows only)","PetscDrawSetSave",movie,&movie,NULL);CHKERRQ(ierr);
3848b7fcac6SBarry Smith     ierr = PetscOptionsBool("-draw_save_single_file","Each new image replaces previous image in file","PetscDrawSetSave",draw->savesinglefile,&draw->savesinglefile,NULL);CHKERRQ(ierr);
3855c6c1daeSBarry Smith     ierr = PetscOptionsString("-draw_save","Save graphics to file (X Windows only)","PetscDrawSetSave",filename,filename,PETSC_MAX_PATH_LEN,&save);CHKERRQ(ierr);
3865c6c1daeSBarry Smith     if (save) {
3875c6c1daeSBarry Smith       ierr = PetscDrawSetSave(draw,filename,movie);CHKERRQ(ierr);
3885c6c1daeSBarry Smith     }
389287de1a7SBarry Smith     ierr = PetscOptionsString("-draw_save_final_image","Save graphics to file (X Windows only)","PetscDrawSetSaveFinalImage",filename,filename,PETSC_MAX_PATH_LEN,&save);CHKERRQ(ierr);
390287de1a7SBarry Smith     if (save) {
391287de1a7SBarry Smith       ierr = PetscDrawSetSaveFinalImage(draw,filename);CHKERRQ(ierr);
392287de1a7SBarry Smith     }
393a4494fc1SBarry Smith     ierr = PetscOptionsBool("-draw_save_on_flush","Save graphics to file (X Windows only) on each flush","PetscDrawSetSave",draw->saveonflush,&draw->saveonflush,NULL);CHKERRQ(ierr);
3945c6c1daeSBarry Smith   }
3955c6c1daeSBarry Smith #endif
3967450148dSBarry Smith   ierr = PetscOptionsGetReal(NULL,"-draw_pause",&dpause,&flg);CHKERRQ(ierr);
3977450148dSBarry Smith   if (flg) draw->pause = dpause;
3985c6c1daeSBarry Smith 
39973f7a4c5SBarry Smith   ierr = PetscOptionsEnum("-draw_marker_type","Type of marker to use on plots","PetscDrawSetMarkerType",PetscDrawMarkerTypes,(PetscEnum)draw->markertype,(PetscEnum *)&draw->markertype,NULL);CHKERRQ(ierr);
40073f7a4c5SBarry Smith 
4015c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
4025c6c1daeSBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject)draw);CHKERRQ(ierr);
403ce1779c8SBarry Smith 
404ce1779c8SBarry Smith   ierr = PetscDrawViewFromOptions(draw,NULL,"-draw_view");CHKERRQ(ierr);
4055c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
4065c6c1daeSBarry Smith   PetscFunctionReturn(0);
4075c6c1daeSBarry Smith }
4085c6c1daeSBarry Smith 
4095c6c1daeSBarry Smith #undef __FUNCT__
4105c6c1daeSBarry Smith #define __FUNCT__ "PetscDrawSetSave"
4115c6c1daeSBarry Smith /*@C
41299bbeb84SJed Brown    PetscDrawSetSave - Saves images produced in a PetscDraw into a file as a Gif file using AfterImage
4135c6c1daeSBarry Smith 
4145c6c1daeSBarry Smith    Collective on PetscDraw
4155c6c1daeSBarry Smith 
4165c6c1daeSBarry Smith    Input Parameter:
4175c6c1daeSBarry Smith +  draw      - the graphics context
41876f01e85SBarry Smith .  filename  - name of the file, if .ext then uses name of draw object plus .ext using .ext to determine the image type, if NULL use .Gif image type
4195c6c1daeSBarry Smith -  movie - produce a movie of all the images
4205c6c1daeSBarry Smith 
4215c6c1daeSBarry Smith    Options Database Command:
42276f01e85SBarry Smith +  -draw_save  <filename> - filename could be name.ext or .ext (where .ext determines the type of graphics file to save, for example .Gif)
423236f5a4dSBarry Smith .  -draw_save_movie
424236f5a4dSBarry Smith .  -draw_save_final_image [optional filename] - (X windows only) saves the final image displayed in a window
425236f5a4dSBarry Smith .  -draw_save_on_flush - saves an image on each flush in addition to each clear
426236f5a4dSBarry Smith -  -draw_save_single_file - saves each new image in the same file, normally each new image is saved in a new file with filename_%d
4275c6c1daeSBarry Smith 
4285c6c1daeSBarry Smith    Level: intermediate
4295c6c1daeSBarry Smith 
4305c6c1daeSBarry Smith    Concepts: X windows^graphics
4315c6c1daeSBarry Smith 
4325c6c1daeSBarry Smith    Notes: You should call this BEFORE calling PetscDrawClear() and creating your image.
4335c6c1daeSBarry Smith 
4345c6c1daeSBarry Smith    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
4355c6c1daeSBarry Smith 
43676f01e85SBarry Smith    The .ext formats that are supported depend on what formats AfterImage was configured with; on the Apple Mac both .Gif and .Jpeg are supported.
43776f01e85SBarry Smith 
4385c6c1daeSBarry Smith    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the
4395c6c1daeSBarry Smith    ./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
4405c6c1daeSBarry Smith 
4415c6c1daeSBarry Smith 
442287de1a7SBarry Smith .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSaveFinalImage()
4435c6c1daeSBarry Smith @*/
4445c6c1daeSBarry Smith PetscErrorCode  PetscDrawSetSave(PetscDraw draw,const char *filename,PetscBool movie)
4455c6c1daeSBarry Smith {
4465c6c1daeSBarry Smith   PetscErrorCode ierr;
44776f01e85SBarry Smith   char           *ext;
4485c6c1daeSBarry Smith 
4495c6c1daeSBarry Smith   PetscFunctionBegin;
4505c6c1daeSBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
4515c6c1daeSBarry Smith   ierr = PetscFree(draw->savefilename);CHKERRQ(ierr);
452a297a907SKarl Rupp 
45376f01e85SBarry Smith   /* determine extension of filename */
45476f01e85SBarry Smith   if (filename && filename[0]) {
45576f01e85SBarry Smith     ierr = PetscStrchr(filename,'.',&ext);CHKERRQ(ierr);
45676f01e85SBarry Smith     if (!ext) SETERRQ1(PetscObjectComm((PetscObject)draw),PETSC_ERR_ARG_INCOMP,"Filename %s should end with graphics extension (for example .Gif)",filename);
45776f01e85SBarry Smith   } else {
45876f01e85SBarry Smith     ext = (char *)".Gif";
45976f01e85SBarry Smith   }
46076f01e85SBarry Smith   if (ext == filename) filename = NULL;
46176f01e85SBarry Smith   ierr = PetscStrallocpy(ext,&draw->savefilenameext);CHKERRQ(ierr);
46276f01e85SBarry Smith 
4635c6c1daeSBarry Smith   draw->savefilemovie = movie;
4645c6c1daeSBarry Smith   if (filename && filename[0]) {
46576f01e85SBarry Smith     size_t  l1,l2;
46676f01e85SBarry Smith     ierr = PetscStrlen(filename,&l1);CHKERRQ(ierr);
46776f01e85SBarry Smith     ierr = PetscStrlen(ext,&l2);CHKERRQ(ierr);
46876f01e85SBarry Smith     ierr = PetscMalloc1(l1-l2+1,&draw->savefilename);CHKERRQ(ierr);
46976f01e85SBarry Smith     ierr = PetscStrncpy(draw->savefilename,filename,l1-l2+1);CHKERRQ(ierr);
4705c6c1daeSBarry Smith   } else {
4715c6c1daeSBarry Smith     const char *name;
47276f01e85SBarry Smith 
4735c6c1daeSBarry Smith     ierr = PetscObjectGetName((PetscObject)draw,&name);CHKERRQ(ierr);
4745c6c1daeSBarry Smith     ierr = PetscStrallocpy(name,&draw->savefilename);CHKERRQ(ierr);
4755c6c1daeSBarry Smith   }
47676f01e85SBarry Smith   ierr = PetscInfo2(NULL,"Will save images to file %s%s\n",draw->savefilename,draw->savefilenameext);CHKERRQ(ierr);
4775c6c1daeSBarry Smith   if (draw->ops->setsave) {
478287de1a7SBarry Smith     ierr = (*draw->ops->setsave)(draw,draw->savefilename);CHKERRQ(ierr);
479287de1a7SBarry Smith   }
480287de1a7SBarry Smith   PetscFunctionReturn(0);
481287de1a7SBarry Smith }
482287de1a7SBarry Smith 
483287de1a7SBarry Smith #undef __FUNCT__
484287de1a7SBarry Smith #define __FUNCT__ "PetscDrawSetSaveFinalImage"
485287de1a7SBarry Smith /*@C
486287de1a7SBarry Smith    PetscDrawSetSaveFinalImage - Saves the finale image produced in a PetscDraw into a file as a Gif file using AfterImage
487287de1a7SBarry Smith 
488287de1a7SBarry Smith    Collective on PetscDraw
489287de1a7SBarry Smith 
490287de1a7SBarry Smith    Input Parameter:
491287de1a7SBarry Smith +  draw      - the graphics context
492287de1a7SBarry Smith -  filename  - name of the file, if NULL uses name of draw object
493287de1a7SBarry Smith 
494287de1a7SBarry Smith    Options Database Command:
495287de1a7SBarry Smith .  -draw_save_final_image  <filename>
496287de1a7SBarry Smith 
497287de1a7SBarry Smith    Level: intermediate
498287de1a7SBarry Smith 
499287de1a7SBarry Smith    Concepts: X windows^graphics
500287de1a7SBarry Smith 
501287de1a7SBarry Smith    Notes: You should call this BEFORE calling PetscDrawClear() and creating your image.
502287de1a7SBarry Smith 
503287de1a7SBarry Smith    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
504287de1a7SBarry Smith 
505287de1a7SBarry Smith    If X windows generates an error message about X_CreateWindow() failing then Afterimage was installed without X windows. Reinstall Afterimage using the
506287de1a7SBarry Smith    ./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
507287de1a7SBarry Smith 
508287de1a7SBarry Smith 
509287de1a7SBarry Smith .seealso: PetscDrawSetFromOptions(), PetscDrawCreate(), PetscDrawDestroy(), PetscDrawSetSave()
510287de1a7SBarry Smith @*/
511287de1a7SBarry Smith PetscErrorCode  PetscDrawSetSaveFinalImage(PetscDraw draw,const char *filename)
512287de1a7SBarry Smith {
513287de1a7SBarry Smith   PetscErrorCode ierr;
514287de1a7SBarry Smith 
515287de1a7SBarry Smith   PetscFunctionBegin;
516287de1a7SBarry Smith   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
517287de1a7SBarry Smith   ierr = PetscFree(draw->savefinalfilename);CHKERRQ(ierr);
518287de1a7SBarry Smith 
519287de1a7SBarry Smith   if (filename && filename[0]) {
520287de1a7SBarry Smith     ierr = PetscStrallocpy(filename,&draw->savefinalfilename);CHKERRQ(ierr);
521287de1a7SBarry Smith   } else {
522287de1a7SBarry Smith     const char *name;
523287de1a7SBarry Smith     ierr = PetscObjectGetName((PetscObject)draw,&name);CHKERRQ(ierr);
524287de1a7SBarry Smith     ierr = PetscStrallocpy(name,&draw->savefinalfilename);CHKERRQ(ierr);
5255c6c1daeSBarry Smith   }
5265c6c1daeSBarry Smith   PetscFunctionReturn(0);
5275c6c1daeSBarry Smith }
528