xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 2bf49c77dedd4b28e6efd10dcebd37482390fa09)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>  /*I "petscsys.h" I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith PetscFList PetscViewerList              = 0;
55c6c1daeSBarry Smith 
6*2bf49c77SBarry Smith PetscErrorCode PetscOptionsFindPair_Private(const char[],const char[],char *[],PetscBool*);
7*2bf49c77SBarry Smith #undef __FUNCT__
8*2bf49c77SBarry Smith #define __FUNCT__ "PetscOptionsGetViewer"
9*2bf49c77SBarry Smith /*@C
10*2bf49c77SBarry Smith    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
11*2bf49c77SBarry Smith 
12*2bf49c77SBarry Smith    Collective on MPI_Comm
13*2bf49c77SBarry Smith 
14*2bf49c77SBarry Smith    Input Parameters:
15*2bf49c77SBarry Smith +  comm - the communicator to own the viewer
16*2bf49c77SBarry Smith .  pre - the string to prepend to the name or PETSC_NULL
17*2bf49c77SBarry Smith -  name - the option one is seeking
18*2bf49c77SBarry Smith 
19*2bf49c77SBarry Smith    Output Parameter:
20*2bf49c77SBarry Smith +  viewer - the viewer
21*2bf49c77SBarry Smith -  set - PETSC_TRUE if found, else PETSC_FALSE
22*2bf49c77SBarry Smith 
23*2bf49c77SBarry Smith    Level: intermediate
24*2bf49c77SBarry Smith 
25*2bf49c77SBarry Smith    Notes: If no value is provided ascii:stdout is used
26*2bf49c77SBarry Smith $       ascii[:filename]    defaults to stdout
27*2bf49c77SBarry Smith $       binary[:filename]   defaults to binaryoutput
28*2bf49c77SBarry Smith $       draw
29*2bf49c77SBarry Smith $       socket[:port]    defaults to the standard output port
30*2bf49c77SBarry Smith 
31*2bf49c77SBarry Smith    Use PetscOptionsRestoreViewer() after using the viewer, otherwise a memory leak may occur
32*2bf49c77SBarry Smith 
33*2bf49c77SBarry Smith .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
34*2bf49c77SBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
35*2bf49c77SBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
36*2bf49c77SBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
37*2bf49c77SBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
38*2bf49c77SBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
39*2bf49c77SBarry Smith           PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer()
40*2bf49c77SBarry Smith @*/
41*2bf49c77SBarry Smith PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscBool  *set)
42*2bf49c77SBarry Smith {
43*2bf49c77SBarry Smith   char           *value;
44*2bf49c77SBarry Smith   PetscErrorCode ierr;
45*2bf49c77SBarry Smith   PetscBool      flag;
46*2bf49c77SBarry Smith 
47*2bf49c77SBarry Smith   PetscFunctionBegin;
48*2bf49c77SBarry Smith   PetscValidCharPointer(name,3);
49*2bf49c77SBarry Smith 
50*2bf49c77SBarry Smith   if (set) *set = PETSC_FALSE;
51*2bf49c77SBarry Smith   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
52*2bf49c77SBarry Smith   if (flag) {
53*2bf49c77SBarry Smith     if (set) *set = PETSC_TRUE;
54*2bf49c77SBarry Smith     if (!value) {
55*2bf49c77SBarry Smith       ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
56*2bf49c77SBarry Smith     } else {
57*2bf49c77SBarry Smith       PetscBool         isbinary = PETSC_FALSE,isascii = PETSC_FALSE;
58*2bf49c77SBarry Smith       char              *cvalue,*loc;
59*2bf49c77SBarry Smith       PetscInt          cnt;
60*2bf49c77SBarry Smith       const char  const *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,0};
61*2bf49c77SBarry Smith 
62*2bf49c77SBarry Smith       ierr = PetscStrallocpy(value,&cvalue);CHKERRQ(ierr);
63*2bf49c77SBarry Smith       ierr = PetscStrchr(cvalue,':',&loc);CHKERRQ(ierr);
64*2bf49c77SBarry Smith       if (loc) {*loc = 0; loc++;}
65*2bf49c77SBarry Smith       ierr = PetscStrendswithwhich(cvalue,viewers,&cnt);CHKERRQ(ierr);
66*2bf49c77SBarry Smith       if (cnt == 4) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",cvalue);
67*2bf49c77SBarry Smith       if (!loc) {
68*2bf49c77SBarry Smith         ierr = PetscStrcmp(cvalue,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
69*2bf49c77SBarry Smith         if (isbinary) {
70*2bf49c77SBarry Smith           *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr);
71*2bf49c77SBarry Smith           ierr = PetscFree(cvalue);CHKERRQ(ierr);
72*2bf49c77SBarry Smith           PetscFunctionReturn(0);
73*2bf49c77SBarry Smith         }
74*2bf49c77SBarry Smith         ierr = PetscStrcmp(cvalue,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
75*2bf49c77SBarry Smith         if (isascii) {
76*2bf49c77SBarry Smith           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
77*2bf49c77SBarry Smith           ierr = PetscFree(cvalue);CHKERRQ(ierr);
78*2bf49c77SBarry Smith           PetscFunctionReturn(0);
79*2bf49c77SBarry Smith         }
80*2bf49c77SBarry Smith       }
81*2bf49c77SBarry Smith       ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
82*2bf49c77SBarry Smith       ierr = PetscViewerSetType(*viewer,cvalue);CHKERRQ(ierr);
83*2bf49c77SBarry Smith       if (loc) {
84*2bf49c77SBarry Smith         ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
85*2bf49c77SBarry Smith         ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr);
86*2bf49c77SBarry Smith       }
87*2bf49c77SBarry Smith       ierr = PetscObjectComposeFunction((PetscObject)*viewer,"PetscOptionsDestroyViewer","PetscViewerDestroy",(void (*)(void))PetscViewerDestroy);CHKERRQ(ierr);
88*2bf49c77SBarry Smith       ierr = PetscFree(cvalue);CHKERRQ(ierr);
89*2bf49c77SBarry Smith       ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
90*2bf49c77SBarry Smith     }
91*2bf49c77SBarry Smith   }
92*2bf49c77SBarry Smith   PetscFunctionReturn(0);
93*2bf49c77SBarry Smith }
94*2bf49c77SBarry Smith 
95*2bf49c77SBarry Smith #undef __FUNCT__
96*2bf49c77SBarry Smith #define __FUNCT__ "PetscOptionsRestoreViewer"
97*2bf49c77SBarry Smith /*@C
98*2bf49c77SBarry Smith    PetscOptionsRestoresViewer - Restores a viewer obtained with PetscOptionsGetViewer()
99*2bf49c77SBarry Smith 
100*2bf49c77SBarry Smith    Collective on PetscViewer
101*2bf49c77SBarry Smith 
102*2bf49c77SBarry Smith    Input Parameters:
103*2bf49c77SBarry Smith .  viewer - the viewer
104*2bf49c77SBarry Smith 
105*2bf49c77SBarry Smith    Level: intermediate
106*2bf49c77SBarry Smith 
107*2bf49c77SBarry Smith .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
108*2bf49c77SBarry Smith           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
109*2bf49c77SBarry Smith           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
110*2bf49c77SBarry Smith           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
111*2bf49c77SBarry Smith           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
112*2bf49c77SBarry Smith           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
113*2bf49c77SBarry Smith           PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer()
114*2bf49c77SBarry Smith @*/
115*2bf49c77SBarry Smith PetscErrorCode  PetscOptionsRestoreViewer(PetscViewer viewer)
116*2bf49c77SBarry Smith {
117*2bf49c77SBarry Smith   PetscErrorCode ierr,(*f)(PetscViewer*);
118*2bf49c77SBarry Smith 
119*2bf49c77SBarry Smith   PetscFunctionBegin;
120*2bf49c77SBarry Smith   ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsDestroyViewer",(void (**)(void))&f);CHKERRQ(ierr);
121*2bf49c77SBarry Smith   if (f) {
122*2bf49c77SBarry Smith     ierr = (*f)(&viewer);CHKERRQ(ierr);
123*2bf49c77SBarry Smith   }
124*2bf49c77SBarry Smith   PetscFunctionReturn(0);
125*2bf49c77SBarry Smith }
126*2bf49c77SBarry Smith 
1275c6c1daeSBarry Smith #undef __FUNCT__
1285c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate"
1295c6c1daeSBarry Smith /*@
1305c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith    Collective on MPI_Comm
1335c6c1daeSBarry Smith 
1345c6c1daeSBarry Smith    Input Parameter:
1355c6c1daeSBarry Smith .  comm - MPI communicator
1365c6c1daeSBarry Smith 
1375c6c1daeSBarry Smith    Output Parameter:
1385c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith    Level: advanced
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith    Concepts: graphics^creating PetscViewer
1435c6c1daeSBarry Smith    Concepts: file input/output^creating PetscViewer
1445c6c1daeSBarry Smith    Concepts: sockets^creating PetscViewer
1455c6c1daeSBarry Smith 
1465c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith @*/
1495c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
1505c6c1daeSBarry Smith {
1515c6c1daeSBarry Smith   PetscViewer    viewer;
1525c6c1daeSBarry Smith   PetscErrorCode ierr;
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith   PetscFunctionBegin;
1555c6c1daeSBarry Smith   *inviewer = 0;
1565c6c1daeSBarry Smith #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
1575c6c1daeSBarry Smith   ierr = PetscViewerInitializePackage(PETSC_NULL);CHKERRQ(ierr);
1585c6c1daeSBarry Smith #endif
1595c6c1daeSBarry Smith   ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr);
1605c6c1daeSBarry Smith   *inviewer           = viewer;
1615c6c1daeSBarry Smith   viewer->data        = 0;
1625c6c1daeSBarry Smith   PetscFunctionReturn(0);
1635c6c1daeSBarry Smith }
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith #undef __FUNCT__
1665c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetType"
1675c6c1daeSBarry Smith /*@C
1685c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith    Collective on PetscViewer
1715c6c1daeSBarry Smith 
1725c6c1daeSBarry Smith    Input Parameter:
1735c6c1daeSBarry Smith +  viewer      - the PetscViewer context
1745c6c1daeSBarry Smith -  type        - for example, "ASCII"
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith    Options Database Command:
1775c6c1daeSBarry Smith .  -draw_type  <type> - Sets the type; use -help for a list
1785c6c1daeSBarry Smith     of available methods (for instance, ascii)
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith    Level: advanced
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith    Notes:
1835c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
1845c6c1daeSBarry Smith    PETSC_VIEWER_SOCKET)
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType
1875c6c1daeSBarry Smith @*/
1885c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
1895c6c1daeSBarry Smith {
1905c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscViewer);
1915c6c1daeSBarry Smith   PetscBool      match;
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith   PetscFunctionBegin;
1945c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1955c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
1965c6c1daeSBarry Smith   CHKMEMQ;
1975c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
1985c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith   /* cleanup any old type that may be there */
2015c6c1daeSBarry Smith   if (viewer->data) {
2025c6c1daeSBarry Smith     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
2035c6c1daeSBarry Smith     viewer->ops->destroy = PETSC_NULL;
2045c6c1daeSBarry Smith     viewer->data = 0;
2055c6c1daeSBarry Smith   }
2065c6c1daeSBarry Smith   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith   ierr =  PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
2095c6c1daeSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
2105c6c1daeSBarry Smith 
2115c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
2125c6c1daeSBarry Smith   ierr = (*r)(viewer);CHKERRQ(ierr);
2135c6c1daeSBarry Smith   PetscFunctionReturn(0);
2145c6c1daeSBarry Smith }
2155c6c1daeSBarry Smith 
2165c6c1daeSBarry Smith #undef __FUNCT__
2175c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegisterDestroy"
2185c6c1daeSBarry Smith /*@C
2195c6c1daeSBarry Smith    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
2205c6c1daeSBarry Smith    registered by PetscViewerRegisterDynamic().
2215c6c1daeSBarry Smith 
2225c6c1daeSBarry Smith    Not Collective
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith    Level: developer
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
2275c6c1daeSBarry Smith @*/
2285c6c1daeSBarry Smith PetscErrorCode  PetscViewerRegisterDestroy(void)
2295c6c1daeSBarry Smith {
2305c6c1daeSBarry Smith   PetscErrorCode ierr;
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith   PetscFunctionBegin;
2335c6c1daeSBarry Smith   ierr = PetscFListDestroy(&PetscViewerList);CHKERRQ(ierr);
2345c6c1daeSBarry Smith   PetscFunctionReturn(0);
2355c6c1daeSBarry Smith }
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith #undef __FUNCT__
2385c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegister"
2395c6c1daeSBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
2405c6c1daeSBarry Smith {
2415c6c1daeSBarry Smith   PetscErrorCode ierr;
2425c6c1daeSBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith   PetscFunctionBegin;
2455c6c1daeSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2465c6c1daeSBarry Smith   ierr = PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2475c6c1daeSBarry Smith   PetscFunctionReturn(0);
2485c6c1daeSBarry Smith }
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith #undef __FUNCT__
2515c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetFromOptions"
2525c6c1daeSBarry Smith /*@C
2535c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
2545c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
2555c6c1daeSBarry Smith 
2565c6c1daeSBarry Smith    Collective on PetscViewer
2575c6c1daeSBarry Smith 
2585c6c1daeSBarry Smith    Input Parameter:
2595c6c1daeSBarry Smith .     PetscViewer - the graphics context
2605c6c1daeSBarry Smith 
2615c6c1daeSBarry Smith    Level: intermediate
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith    Notes:
2645c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith   Concepts: PetscViewer^setting options
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith @*/
2715c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
2725c6c1daeSBarry Smith {
2735c6c1daeSBarry Smith   PetscErrorCode ierr;
2745c6c1daeSBarry Smith   char       vtype[256];
2755c6c1daeSBarry Smith   PetscBool  flg;
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith   PetscFunctionBegin;
2785c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith   if (!PetscViewerList) {
2815c6c1daeSBarry Smith     ierr = PetscViewerRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2825c6c1daeSBarry Smith   }
2835c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
2845c6c1daeSBarry Smith     ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(((PetscObject)viewer)->type_name?((PetscObject)viewer)->type_name:PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
2855c6c1daeSBarry Smith     if (flg) {
2865c6c1daeSBarry Smith       ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
2875c6c1daeSBarry Smith     }
2885c6c1daeSBarry Smith     /* type has not been set? */
2895c6c1daeSBarry Smith     if (!((PetscObject)viewer)->type_name) {
2905c6c1daeSBarry Smith       ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
2915c6c1daeSBarry Smith     }
2925c6c1daeSBarry Smith     if (viewer->ops->setfromoptions) {
2935c6c1daeSBarry Smith       ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr);
2945c6c1daeSBarry Smith     }
2955c6c1daeSBarry Smith 
2965c6c1daeSBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
2975c6c1daeSBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
2985c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2995c6c1daeSBarry Smith 
3005c6c1daeSBarry Smith   PetscFunctionReturn(0);
3015c6c1daeSBarry Smith }
302