xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 5c6c1daec53e1d9ab0bec9db5309fd8fc7645b8d)
1*5c6c1daeSBarry Smith 
2*5c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>  /*I "petscsys.h" I*/
3*5c6c1daeSBarry Smith 
4*5c6c1daeSBarry Smith PetscFList PetscViewerList              = 0;
5*5c6c1daeSBarry Smith 
6*5c6c1daeSBarry Smith #undef __FUNCT__
7*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate"
8*5c6c1daeSBarry Smith /*@
9*5c6c1daeSBarry Smith    PetscViewerCreate - Creates a viewing context
10*5c6c1daeSBarry Smith 
11*5c6c1daeSBarry Smith    Collective on MPI_Comm
12*5c6c1daeSBarry Smith 
13*5c6c1daeSBarry Smith    Input Parameter:
14*5c6c1daeSBarry Smith .  comm - MPI communicator
15*5c6c1daeSBarry Smith 
16*5c6c1daeSBarry Smith    Output Parameter:
17*5c6c1daeSBarry Smith .  inviewer - location to put the PetscViewer context
18*5c6c1daeSBarry Smith 
19*5c6c1daeSBarry Smith    Level: advanced
20*5c6c1daeSBarry Smith 
21*5c6c1daeSBarry Smith    Concepts: graphics^creating PetscViewer
22*5c6c1daeSBarry Smith    Concepts: file input/output^creating PetscViewer
23*5c6c1daeSBarry Smith    Concepts: sockets^creating PetscViewer
24*5c6c1daeSBarry Smith 
25*5c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
26*5c6c1daeSBarry Smith 
27*5c6c1daeSBarry Smith @*/
28*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
29*5c6c1daeSBarry Smith {
30*5c6c1daeSBarry Smith   PetscViewer    viewer;
31*5c6c1daeSBarry Smith   PetscErrorCode ierr;
32*5c6c1daeSBarry Smith 
33*5c6c1daeSBarry Smith   PetscFunctionBegin;
34*5c6c1daeSBarry Smith   *inviewer = 0;
35*5c6c1daeSBarry Smith #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
36*5c6c1daeSBarry Smith   ierr = PetscViewerInitializePackage(PETSC_NULL);CHKERRQ(ierr);
37*5c6c1daeSBarry Smith #endif
38*5c6c1daeSBarry Smith   ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr);
39*5c6c1daeSBarry Smith   *inviewer           = viewer;
40*5c6c1daeSBarry Smith   viewer->data        = 0;
41*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
42*5c6c1daeSBarry Smith }
43*5c6c1daeSBarry Smith 
44*5c6c1daeSBarry Smith #undef __FUNCT__
45*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetType"
46*5c6c1daeSBarry Smith /*@C
47*5c6c1daeSBarry Smith    PetscViewerSetType - Builds PetscViewer for a particular implementation.
48*5c6c1daeSBarry Smith 
49*5c6c1daeSBarry Smith    Collective on PetscViewer
50*5c6c1daeSBarry Smith 
51*5c6c1daeSBarry Smith    Input Parameter:
52*5c6c1daeSBarry Smith +  viewer      - the PetscViewer context
53*5c6c1daeSBarry Smith -  type        - for example, "ASCII"
54*5c6c1daeSBarry Smith 
55*5c6c1daeSBarry Smith    Options Database Command:
56*5c6c1daeSBarry Smith .  -draw_type  <type> - Sets the type; use -help for a list
57*5c6c1daeSBarry Smith     of available methods (for instance, ascii)
58*5c6c1daeSBarry Smith 
59*5c6c1daeSBarry Smith    Level: advanced
60*5c6c1daeSBarry Smith 
61*5c6c1daeSBarry Smith    Notes:
62*5c6c1daeSBarry Smith    See "include/petscviewer.h" for available methods (for instance,
63*5c6c1daeSBarry Smith    PETSC_VIEWER_SOCKET)
64*5c6c1daeSBarry Smith 
65*5c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType
66*5c6c1daeSBarry Smith @*/
67*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
68*5c6c1daeSBarry Smith {
69*5c6c1daeSBarry Smith   PetscErrorCode ierr,(*r)(PetscViewer);
70*5c6c1daeSBarry Smith   PetscBool      match;
71*5c6c1daeSBarry Smith 
72*5c6c1daeSBarry Smith   PetscFunctionBegin;
73*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
74*5c6c1daeSBarry Smith   PetscValidCharPointer(type,2);
75*5c6c1daeSBarry Smith   CHKMEMQ;
76*5c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
77*5c6c1daeSBarry Smith   if (match) PetscFunctionReturn(0);
78*5c6c1daeSBarry Smith 
79*5c6c1daeSBarry Smith   /* cleanup any old type that may be there */
80*5c6c1daeSBarry Smith   if (viewer->data) {
81*5c6c1daeSBarry Smith     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
82*5c6c1daeSBarry Smith     viewer->ops->destroy = PETSC_NULL;
83*5c6c1daeSBarry Smith     viewer->data = 0;
84*5c6c1daeSBarry Smith   }
85*5c6c1daeSBarry Smith   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
86*5c6c1daeSBarry Smith 
87*5c6c1daeSBarry Smith   ierr =  PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
88*5c6c1daeSBarry Smith   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
89*5c6c1daeSBarry Smith 
90*5c6c1daeSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
91*5c6c1daeSBarry Smith   ierr = (*r)(viewer);CHKERRQ(ierr);
92*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
93*5c6c1daeSBarry Smith }
94*5c6c1daeSBarry Smith 
95*5c6c1daeSBarry Smith #undef __FUNCT__
96*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegisterDestroy"
97*5c6c1daeSBarry Smith /*@C
98*5c6c1daeSBarry Smith    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
99*5c6c1daeSBarry Smith    registered by PetscViewerRegisterDynamic().
100*5c6c1daeSBarry Smith 
101*5c6c1daeSBarry Smith    Not Collective
102*5c6c1daeSBarry Smith 
103*5c6c1daeSBarry Smith    Level: developer
104*5c6c1daeSBarry Smith 
105*5c6c1daeSBarry Smith .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
106*5c6c1daeSBarry Smith @*/
107*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerRegisterDestroy(void)
108*5c6c1daeSBarry Smith {
109*5c6c1daeSBarry Smith   PetscErrorCode ierr;
110*5c6c1daeSBarry Smith 
111*5c6c1daeSBarry Smith   PetscFunctionBegin;
112*5c6c1daeSBarry Smith   ierr = PetscFListDestroy(&PetscViewerList);CHKERRQ(ierr);
113*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
114*5c6c1daeSBarry Smith }
115*5c6c1daeSBarry Smith 
116*5c6c1daeSBarry Smith #undef __FUNCT__
117*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRegister"
118*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
119*5c6c1daeSBarry Smith {
120*5c6c1daeSBarry Smith   PetscErrorCode ierr;
121*5c6c1daeSBarry Smith   char fullname[PETSC_MAX_PATH_LEN];
122*5c6c1daeSBarry Smith 
123*5c6c1daeSBarry Smith   PetscFunctionBegin;
124*5c6c1daeSBarry Smith   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
125*5c6c1daeSBarry Smith   ierr = PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
126*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
127*5c6c1daeSBarry Smith }
128*5c6c1daeSBarry Smith 
129*5c6c1daeSBarry Smith #undef __FUNCT__
130*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerSetFromOptions"
131*5c6c1daeSBarry Smith /*@C
132*5c6c1daeSBarry Smith    PetscViewerSetFromOptions - Sets the graphics type from the options database.
133*5c6c1daeSBarry Smith       Defaults to a PETSc X windows graphics.
134*5c6c1daeSBarry Smith 
135*5c6c1daeSBarry Smith    Collective on PetscViewer
136*5c6c1daeSBarry Smith 
137*5c6c1daeSBarry Smith    Input Parameter:
138*5c6c1daeSBarry Smith .     PetscViewer - the graphics context
139*5c6c1daeSBarry Smith 
140*5c6c1daeSBarry Smith    Level: intermediate
141*5c6c1daeSBarry Smith 
142*5c6c1daeSBarry Smith    Notes:
143*5c6c1daeSBarry Smith     Must be called after PetscViewerCreate() before the PetscViewer is used.
144*5c6c1daeSBarry Smith 
145*5c6c1daeSBarry Smith   Concepts: PetscViewer^setting options
146*5c6c1daeSBarry Smith 
147*5c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
148*5c6c1daeSBarry Smith 
149*5c6c1daeSBarry Smith @*/
150*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
151*5c6c1daeSBarry Smith {
152*5c6c1daeSBarry Smith   PetscErrorCode ierr;
153*5c6c1daeSBarry Smith   char       vtype[256];
154*5c6c1daeSBarry Smith   PetscBool  flg;
155*5c6c1daeSBarry Smith 
156*5c6c1daeSBarry Smith   PetscFunctionBegin;
157*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
158*5c6c1daeSBarry Smith 
159*5c6c1daeSBarry Smith   if (!PetscViewerList) {
160*5c6c1daeSBarry Smith     ierr = PetscViewerRegisterAll(PETSC_NULL);CHKERRQ(ierr);
161*5c6c1daeSBarry Smith   }
162*5c6c1daeSBarry Smith   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
163*5c6c1daeSBarry 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);
164*5c6c1daeSBarry Smith     if (flg) {
165*5c6c1daeSBarry Smith       ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
166*5c6c1daeSBarry Smith     }
167*5c6c1daeSBarry Smith     /* type has not been set? */
168*5c6c1daeSBarry Smith     if (!((PetscObject)viewer)->type_name) {
169*5c6c1daeSBarry Smith       ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
170*5c6c1daeSBarry Smith     }
171*5c6c1daeSBarry Smith     if (viewer->ops->setfromoptions) {
172*5c6c1daeSBarry Smith       ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr);
173*5c6c1daeSBarry Smith     }
174*5c6c1daeSBarry Smith 
175*5c6c1daeSBarry Smith     /* process any options handlers added with PetscObjectAddOptionsHandler() */
176*5c6c1daeSBarry Smith     ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
177*5c6c1daeSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
178*5c6c1daeSBarry Smith 
179*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
180*5c6c1daeSBarry Smith }
181