xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 4d2d963c717fd0cdf7b655ceb4f097eb547f9820)
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]]   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
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;
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           ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr);
117         }
118       }
119       ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
120       if (loc2 && *loc2) {
121         ierr = PetscStrtoupper(loc2);CHKERRQ(ierr);
122         ierr = PetscStrendswithwhich(loc2,PetscViewerFormats,&cnt);CHKERRQ(ierr);
123         if (!PetscViewerFormats[cnt]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2);CHKERRQ(ierr);
124         if (format) *format = (PetscViewerFormat)cnt;
125       }
126       ierr = PetscFree(cvalue);CHKERRQ(ierr);
127     }
128   }
129   PetscFunctionReturn(0);
130 }
131 
132 #undef __FUNCT__
133 #define __FUNCT__ "PetscViewerCreate"
134 /*@
135    PetscViewerCreate - Creates a viewing context
136 
137    Collective on MPI_Comm
138 
139    Input Parameter:
140 .  comm - MPI communicator
141 
142    Output Parameter:
143 .  inviewer - location to put the PetscViewer context
144 
145    Level: advanced
146 
147    Concepts: graphics^creating PetscViewer
148    Concepts: file input/output^creating PetscViewer
149    Concepts: sockets^creating PetscViewer
150 
151 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
152 
153 @*/
154 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
155 {
156   PetscViewer    viewer;
157   PetscErrorCode ierr;
158 
159   PetscFunctionBegin;
160   *inviewer = 0;
161 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
162   ierr = PetscViewerInitializePackage(NULL);CHKERRQ(ierr);
163 #endif
164   ierr         = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr);
165   *inviewer    = viewer;
166   viewer->data = 0;
167   PetscFunctionReturn(0);
168 }
169 
170 #undef __FUNCT__
171 #define __FUNCT__ "PetscViewerSetType"
172 /*@C
173    PetscViewerSetType - Builds PetscViewer for a particular implementation.
174 
175    Collective on PetscViewer
176 
177    Input Parameter:
178 +  viewer      - the PetscViewer context
179 -  type        - for example, "ASCII"
180 
181    Options Database Command:
182 .  -draw_type  <type> - Sets the type; use -help for a list
183     of available methods (for instance, ascii)
184 
185    Level: advanced
186 
187    Notes:
188    See "include/petscviewer.h" for available methods (for instance,
189    PETSC_VIEWER_SOCKET)
190 
191 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType
192 @*/
193 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
194 {
195   PetscErrorCode ierr,(*r)(PetscViewer);
196   PetscBool      match;
197 
198   PetscFunctionBegin;
199   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
200   PetscValidCharPointer(type,2);
201   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
202   if (match) PetscFunctionReturn(0);
203 
204   /* cleanup any old type that may be there */
205   if (viewer->data) {
206     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
207 
208     viewer->ops->destroy = NULL;
209     viewer->data         = 0;
210   }
211   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
212 
213   ierr =  PetscFunctionListFind(PetscObjectComm((PetscObject)viewer),PetscViewerList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr);
214   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
215 
216   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
217   ierr = (*r)(viewer);CHKERRQ(ierr);
218   PetscFunctionReturn(0);
219 }
220 
221 #undef __FUNCT__
222 #define __FUNCT__ "PetscViewerRegisterDestroy"
223 /*@C
224    PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were
225    registered by PetscViewerRegisterDynamic().
226 
227    Not Collective
228 
229    Level: developer
230 
231 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll()
232 @*/
233 PetscErrorCode  PetscViewerRegisterDestroy(void)
234 {
235   PetscErrorCode ierr;
236 
237   PetscFunctionBegin;
238   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
239   PetscFunctionReturn(0);
240 }
241 
242 #undef __FUNCT__
243 #define __FUNCT__ "PetscViewerRegister"
244 PetscErrorCode  PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer))
245 {
246   PetscErrorCode ierr;
247   char           fullname[PETSC_MAX_PATH_LEN];
248 
249   PetscFunctionBegin;
250   ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr);
251   ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
252   PetscFunctionReturn(0);
253 }
254 
255 #undef __FUNCT__
256 #define __FUNCT__ "PetscViewerSetFromOptions"
257 /*@C
258    PetscViewerSetFromOptions - Sets the graphics type from the options database.
259       Defaults to a PETSc X windows graphics.
260 
261    Collective on PetscViewer
262 
263    Input Parameter:
264 .     PetscViewer - the graphics context
265 
266    Level: intermediate
267 
268    Notes:
269     Must be called after PetscViewerCreate() before the PetscViewer is used.
270 
271   Concepts: PetscViewer^setting options
272 
273 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
274 
275 @*/
276 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
277 {
278   PetscErrorCode ierr;
279   char           vtype[256];
280   PetscBool      flg;
281 
282   PetscFunctionBegin;
283   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
284 
285   if (!PetscViewerList) {
286     ierr = PetscViewerRegisterAll(NULL);CHKERRQ(ierr);
287   }
288   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
289   ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
290   if (flg) {
291     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
292   }
293   /* type has not been set? */
294   if (!((PetscObject)viewer)->type_name) {
295     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
296   }
297   if (viewer->ops->setfromoptions) {
298     ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr);
299   }
300 
301   /* process any options handlers added with PetscObjectAddOptionsHandler() */
302   ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
303   ierr = PetscOptionsEnd();CHKERRQ(ierr);
304   PetscFunctionReturn(0);
305 }
306