xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 05315717df321390de25b5fc909bd643f9e43bda)
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       *cvalue,*loc,*loc2 = NULL,*loc3 = NULL;
65       PetscInt   cnt;
66       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERAMS,PETSCVIEWERVTK,0};
67 
68       ierr = PetscStrallocpy(value,&cvalue);CHKERRQ(ierr);
69       ierr = PetscStrchr(cvalue,':',&loc);CHKERRQ(ierr);
70       if (loc) {*loc = 0; loc++;}
71       ierr = PetscStrendswithwhich(*cvalue ? cvalue : "ascii",viewers,&cnt);CHKERRQ(ierr);
72       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",cvalue);
73       if (!loc) {
74         switch (cnt) {
75         case 0:
76           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
77           break;
78         case 1:
79           *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr);
80           break;
81         case 2:
82           *viewer = PETSC_VIEWER_DRAW_(comm);CHKERRQ(ierr);
83           break;
84 #if defined(PETSC_USE_SOCKET_VIEWER)
85         case 3:
86           *viewer = PETSC_VIEWER_SOCKET_(comm);CHKERRQ(ierr);
87           break;
88 #endif
89 #if defined(PETSC_HAVE_MATLAB_ENGINE)
90         case 4:
91           *viewer = PETSC_VIEWER_MATLAB_(comm);CHKERRQ(ierr);
92           break;
93 #endif
94 #if defined(PETSC_HAVE_AMS)
95         case 5:
96           *viewer = PETSC_VIEWER_AMS_(comm);CHKERRQ(ierr);
97           break;
98 #endif
99         default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",cvalue);CHKERRQ(ierr);
100           break;
101         }
102         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
103       } else {
104         ierr = PetscStrchr(loc,':',&loc2);CHKERRQ(ierr);
105         if (loc2) {*loc2 = 0; loc2++;}
106         if (loc2 && !*loc && (cnt == 0)) { /* ASCII format without file name */
107           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
108           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
109         } else {
110           ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
111           ierr = PetscViewerSetType(*viewer,*cvalue ? cvalue : "ascii");CHKERRQ(ierr);
112 #if defined(PETSC_HAVE_AMS)
113           ierr = PetscViewerAMSSetCommName(*viewer,loc);CHKERRQ(ierr);
114 #endif
115           ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr);
116           if (cnt == 0) { /* Allow append for ASCII files */
117             ierr = PetscStrchr(loc2,':',&loc3);CHKERRQ(ierr);
118             if (loc3) {*loc3 = 0; loc3++;}
119             if (loc3 && *loc3) {
120               const char *modes[] = {"w","write","a","append"};
121               ierr = PetscStrtolower(loc3);CHKERRQ(ierr);
122               ierr = PetscStrendswithwhich(loc3,modes,&cnt);CHKERRQ(ierr);
123               if (cnt > (PetscInt) sizeof(modes)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Invalid viewer file mode: %s",loc3);
124               switch (cnt) {
125               case 2:
126               case 3:
127                 ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_APPEND);CHKERRQ(ierr);
128                 break;
129               default:
130                 break;
131               }
132             }
133           }
134           ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr);
135         }
136       }
137       ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
138       if (loc2 && *loc2) {
139         ierr = PetscStrtoupper(loc2);CHKERRQ(ierr);
140         ierr = PetscStrendswithwhich(loc2,PetscViewerFormats,&cnt);CHKERRQ(ierr);
141         if (!PetscViewerFormats[cnt]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2);CHKERRQ(ierr);
142         if (format) *format = (PetscViewerFormat)cnt;
143       }
144       ierr = PetscFree(cvalue);CHKERRQ(ierr);
145     }
146   }
147   PetscFunctionReturn(0);
148 }
149 
150 #undef __FUNCT__
151 #define __FUNCT__ "PetscViewerCreate"
152 /*@
153    PetscViewerCreate - Creates a viewing context
154 
155    Collective on MPI_Comm
156 
157    Input Parameter:
158 .  comm - MPI communicator
159 
160    Output Parameter:
161 .  inviewer - location to put the PetscViewer context
162 
163    Level: advanced
164 
165    Concepts: graphics^creating PetscViewer
166    Concepts: file input/output^creating PetscViewer
167    Concepts: sockets^creating PetscViewer
168 
169 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
170 
171 @*/
172 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
173 {
174   PetscViewer    viewer;
175   PetscErrorCode ierr;
176 
177   PetscFunctionBegin;
178   *inviewer = 0;
179 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
180   ierr = PetscViewerInitializePackage(NULL);CHKERRQ(ierr);
181 #endif
182   ierr         = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr);
183   *inviewer    = viewer;
184   viewer->data = 0;
185   PetscFunctionReturn(0);
186 }
187 
188 #undef __FUNCT__
189 #define __FUNCT__ "PetscViewerSetType"
190 /*@C
191    PetscViewerSetType - Builds PetscViewer for a particular implementation.
192 
193    Collective on PetscViewer
194 
195    Input Parameter:
196 +  viewer      - the PetscViewer context
197 -  type        - for example, "ASCII"
198 
199    Options Database Command:
200 .  -draw_type  <type> - Sets the type; use -help for a list
201     of available methods (for instance, ascii)
202 
203    Level: advanced
204 
205    Notes:
206    See "include/petscviewer.h" for available methods (for instance,
207    PETSC_VIEWER_SOCKET)
208 
209 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType
210 @*/
211 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
212 {
213   PetscErrorCode ierr,(*r)(PetscViewer);
214   PetscBool      match;
215 
216   PetscFunctionBegin;
217   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
218   PetscValidCharPointer(type,2);
219   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
220   if (match) PetscFunctionReturn(0);
221 
222   /* cleanup any old type that may be there */
223   if (viewer->data) {
224     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
225 
226     viewer->ops->destroy = NULL;
227     viewer->data         = 0;
228   }
229   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
230 
231   ierr =  PetscFunctionListFind(PetscObjectComm((PetscObject)viewer),PetscViewerList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
232   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
233 
234   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
235   ierr = (*r)(viewer);CHKERRQ(ierr);
236   PetscFunctionReturn(0);
237 }
238 
239 #undef __FUNCT__
240 #define __FUNCT__ "PetscViewerRegisterDestroy"
241 /*@C
242    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
243    registered by PetscViewerRegisterDynamic().
244 
245    Not Collective
246 
247    Level: developer
248 
249 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
250 @*/
251 PetscErrorCode  PetscViewerRegisterDestroy(void)
252 {
253   PetscErrorCode ierr;
254 
255   PetscFunctionBegin;
256   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
257   PetscFunctionReturn(0);
258 }
259 
260 #undef __FUNCT__
261 #define __FUNCT__ "PetscViewerRegister"
262 PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
263 {
264   PetscErrorCode ierr;
265   char           fullname[PETSC_MAX_PATH_LEN];
266 
267   PetscFunctionBegin;
268   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
269   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
270   PetscFunctionReturn(0);
271 }
272 
273 #undef __FUNCT__
274 #define __FUNCT__ "PetscViewerSetFromOptions"
275 /*@C
276    PetscViewerSetFromOptions - Sets the graphics type from the options database.
277       Defaults to a PETSc X windows graphics.
278 
279    Collective on PetscViewer
280 
281    Input Parameter:
282 .     PetscViewer - the graphics context
283 
284    Level: intermediate
285 
286    Notes:
287     Must be called after PetscViewerCreate() before the PetscViewer is used.
288 
289   Concepts: PetscViewer^setting options
290 
291 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
292 
293 @*/
294 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
295 {
296   PetscErrorCode ierr;
297   char           vtype[256];
298   PetscBool      flg;
299 
300   PetscFunctionBegin;
301   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
302 
303   if (!PetscViewerList) {
304     ierr = PetscViewerRegisterAll(NULL);CHKERRQ(ierr);
305   }
306   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
307   ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
308   if (flg) {
309     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
310   }
311   /* type has not been set? */
312   if (!((PetscObject)viewer)->type_name) {
313     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
314   }
315   if (viewer->ops->setfromoptions) {
316     ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr);
317   }
318 
319   /* process any options handlers added with PetscObjectAddOptionsHandler() */
320   ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
321   ierr = PetscOptionsEnd();CHKERRQ(ierr);
322   PetscFunctionReturn(0);
323 }
324