xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 35d27ee359274c7c058527937f060f850ffed590)
1 
2 #include <petsc-private/viewerimpl.h>  /*I "petscviewer.h" I*/
3 #if defined(PETSC_HAVE_AMS)
4 #include <petscviewerams.h>
5 #endif
6 
7 PetscFunctionList PetscViewerList = 0;
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "PetscOptionsGetViewer"
11 /*@C
12    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
13 
14    Collective on MPI_Comm
15 
16    Input Parameters:
17 +  comm - the communicator to own the viewer
18 .  pre - the string to prepend to the name or NULL
19 -  name - the option one is seeking
20 
21    Output Parameter:
22 +  viewer - the viewer
23 .  format - the PetscViewerFormat requested by the user
24 -  set - PETSC_TRUE if found, else PETSC_FALSE
25 
26    Level: intermediate
27 
28    Notes: If no value is provided ascii:stdout is used
29 $       ascii[:[filename][:[format][:append]]   defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab, for example ascii::ascii_info prints just the info
30 $                                     about the object to standard out - unless :append is given filename opens in write mode
31 $       binary[:[filename][:format]]   defaults to binaryoutput
32 $       draw
33 $       socket[:port]                  defaults to the standard output port
34 $       ams[:communicatorname]         publishes object to the AMS (Argonne Memory Snooper)
35 
36    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
37 
38 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
39           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
40           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
41           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
42           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
43           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
44           PetscOptionsList(), PetscOptionsEList()
45 @*/
46 PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
47 {
48   char           *value;
49   PetscErrorCode ierr;
50   PetscBool      flag;
51 
52   PetscFunctionBegin;
53   PetscValidCharPointer(name,3);
54 
55   if (format) *format = PETSC_VIEWER_DEFAULT;
56   if (set) *set = PETSC_FALSE;
57   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
58   if (flag) {
59     if (set) *set = PETSC_TRUE;
60     if (!value) {
61       ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
62       ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
63     } else {
64       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
65       PetscInt   cnt;
66       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERAMS,PETSCVIEWERVTK,0};
67 
68       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
69       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
70       if (loc1_fname) {
71         *loc1_fname++ = 0;
72         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
73       }
74       if (loc2_fmt) {
75         *loc2_fmt++ = 0;
76         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
77       }
78       if (loc3_fmode) *loc3_fmode++ = 0;
79       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
80       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
81       if (!loc1_fname) {
82         switch (cnt) {
83         case 0:
84           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
85           break;
86         case 1:
87           *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr);
88           break;
89         case 2:
90           *viewer = PETSC_VIEWER_DRAW_(comm);CHKERRQ(ierr);
91           break;
92 #if defined(PETSC_USE_SOCKET_VIEWER)
93         case 3:
94           *viewer = PETSC_VIEWER_SOCKET_(comm);CHKERRQ(ierr);
95           break;
96 #endif
97 #if defined(PETSC_HAVE_MATLAB_ENGINE)
98         case 4:
99           *viewer = PETSC_VIEWER_MATLAB_(comm);CHKERRQ(ierr);
100           break;
101 #endif
102 #if defined(PETSC_HAVE_AMS)
103         case 5:
104           *viewer = PETSC_VIEWER_AMS_(comm);CHKERRQ(ierr);
105           break;
106 #endif
107         default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);CHKERRQ(ierr);
108           break;
109         }
110         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
111       } else {
112         if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
113           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
114           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
115         } else {
116           ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
117           ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
118 #if defined(PETSC_HAVE_AMS)
119           ierr = PetscViewerAMSSetCommName(*viewer,loc1_fname);CHKERRQ(ierr);
120 #endif
121           ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
122           if (cnt == 0 && loc3_fmode && *loc3_fmode) { /* Allow append for ASCII files */
123             PetscFileMode fmode;
124             ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
125             if (flag) {ierr = PetscViewerFileSetMode(*viewer,fmode);CHKERRQ(ierr);}
126             else SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
127           }
128           ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
129         }
130       }
131       if (loc2_fmt && *loc2_fmt) {
132         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr);
133         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr);
134       }
135       ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
136       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
137     }
138   }
139   PetscFunctionReturn(0);
140 }
141 
142 #undef __FUNCT__
143 #define __FUNCT__ "PetscViewerCreate"
144 /*@
145    PetscViewerCreate - Creates a viewing context
146 
147    Collective on MPI_Comm
148 
149    Input Parameter:
150 .  comm - MPI communicator
151 
152    Output Parameter:
153 .  inviewer - location to put the PetscViewer context
154 
155    Level: advanced
156 
157    Concepts: graphics^creating PetscViewer
158    Concepts: file input/output^creating PetscViewer
159    Concepts: sockets^creating PetscViewer
160 
161 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
162 
163 @*/
164 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
165 {
166   PetscViewer    viewer;
167   PetscErrorCode ierr;
168 
169   PetscFunctionBegin;
170   *inviewer = 0;
171 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
172   ierr = PetscViewerInitializePackage(NULL);CHKERRQ(ierr);
173 #endif
174   ierr         = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr);
175   *inviewer    = viewer;
176   viewer->data = 0;
177   PetscFunctionReturn(0);
178 }
179 
180 #undef __FUNCT__
181 #define __FUNCT__ "PetscViewerSetType"
182 /*@C
183    PetscViewerSetType - Builds PetscViewer for a particular implementation.
184 
185    Collective on PetscViewer
186 
187    Input Parameter:
188 +  viewer      - the PetscViewer context
189 -  type        - for example, "ASCII"
190 
191    Options Database Command:
192 .  -draw_type  <type> - Sets the type; use -help for a list
193     of available methods (for instance, ascii)
194 
195    Level: advanced
196 
197    Notes:
198    See "include/petscviewer.h" for available methods (for instance,
199    PETSC_VIEWER_SOCKET)
200 
201 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType
202 @*/
203 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
204 {
205   PetscErrorCode ierr,(*r)(PetscViewer);
206   PetscBool      match;
207 
208   PetscFunctionBegin;
209   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
210   PetscValidCharPointer(type,2);
211   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
212   if (match) PetscFunctionReturn(0);
213 
214   /* cleanup any old type that may be there */
215   if (viewer->data) {
216     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
217 
218     viewer->ops->destroy = NULL;
219     viewer->data         = 0;
220   }
221   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
222 
223   ierr =  PetscFunctionListFind(PetscObjectComm((PetscObject)viewer),PetscViewerList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
224   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
225 
226   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
227   ierr = (*r)(viewer);CHKERRQ(ierr);
228   PetscFunctionReturn(0);
229 }
230 
231 #undef __FUNCT__
232 #define __FUNCT__ "PetscViewerRegisterDestroy"
233 /*@C
234    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
235    registered by PetscViewerRegisterDynamic().
236 
237    Not Collective
238 
239    Level: developer
240 
241 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
242 @*/
243 PetscErrorCode  PetscViewerRegisterDestroy(void)
244 {
245   PetscErrorCode ierr;
246 
247   PetscFunctionBegin;
248   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
249   PetscFunctionReturn(0);
250 }
251 
252 #undef __FUNCT__
253 #define __FUNCT__ "PetscViewerRegister"
254 PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
255 {
256   PetscErrorCode ierr;
257   char           fullname[PETSC_MAX_PATH_LEN];
258 
259   PetscFunctionBegin;
260   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
261   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
262   PetscFunctionReturn(0);
263 }
264 
265 #undef __FUNCT__
266 #define __FUNCT__ "PetscViewerSetFromOptions"
267 /*@C
268    PetscViewerSetFromOptions - Sets the graphics type from the options database.
269       Defaults to a PETSc X windows graphics.
270 
271    Collective on PetscViewer
272 
273    Input Parameter:
274 .     PetscViewer - the graphics context
275 
276    Level: intermediate
277 
278    Notes:
279     Must be called after PetscViewerCreate() before the PetscViewer is used.
280 
281   Concepts: PetscViewer^setting options
282 
283 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
284 
285 @*/
286 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
287 {
288   PetscErrorCode ierr;
289   char           vtype[256];
290   PetscBool      flg;
291 
292   PetscFunctionBegin;
293   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
294 
295   if (!PetscViewerList) {
296     ierr = PetscViewerRegisterAll(NULL);CHKERRQ(ierr);
297   }
298   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
299   ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
300   if (flg) {
301     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
302   }
303   /* type has not been set? */
304   if (!((PetscObject)viewer)->type_name) {
305     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
306   }
307   if (viewer->ops->setfromoptions) {
308     ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr);
309   }
310 
311   /* process any options handlers added with PetscObjectAddOptionsHandler() */
312   ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
313   ierr = PetscOptionsEnd();CHKERRQ(ierr);
314   PetscFunctionReturn(0);
315 }
316