xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
1 
2 #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3 #if defined(PETSC_HAVE_SAWS)
4 #include <petscviewersaws.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,
30                                                   for example ascii::ascii_info prints just the information about the object not all details
31                                                   unless :append is given filename opens in write mode, overwriting what was already there
32 $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
33 $       draw[:drawtype]                           for example, draw:tikz  or draw:x
34 $       socket[:port]                             defaults to the standard output port
35 $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
36 
37    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
38 
39 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
40           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
41           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
42           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
43           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
44           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
45           PetscOptionsFList(), PetscOptionsEList()
46 @*/
47 PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
48 {
49   char           *value;
50   PetscErrorCode ierr;
51   PetscBool      flag,hashelp;
52 
53   PetscFunctionBegin;
54   PetscValidCharPointer(name,3);
55 
56   ierr = PetscOptionsHasName(NULL,"-help",&hashelp);CHKERRQ(ierr);
57   if (hashelp) {
58     ierr = (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers display of a PETSc object to screen or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
59     ierr = (*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers saving of a PETSc object to a binary file","PetscOptionsGetViewer");CHKERRQ(ierr);
60     ierr = (*PetscHelpPrintf)(comm,"  -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Triggers drawing of a PETSc object","PetscOptionsGetViewer");CHKERRQ(ierr);
61     ierr = (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Triggers push of a PETSc object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
62     ierr = (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Triggers publishing of a PETSc object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
63   }
64 
65   if (format) *format = PETSC_VIEWER_DEFAULT;
66   if (set) *set = PETSC_FALSE;
67   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
68   if (flag) {
69     if (set) *set = PETSC_TRUE;
70     if (!value) {
71       ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
72       ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
73     } else {
74       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
75       PetscInt   cnt;
76       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};
77 
78       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
79       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
80       if (loc1_fname) {
81         *loc1_fname++ = 0;
82         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
83       }
84       if (loc2_fmt) {
85         *loc2_fmt++ = 0;
86         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
87       }
88       if (loc3_fmode) *loc3_fmode++ = 0;
89       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
90       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
91       if (!loc1_fname) {
92         switch (cnt) {
93         case 0:
94           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
95           break;
96         case 1:
97           if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
98           break;
99         case 2:
100           if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
101           break;
102 #if defined(PETSC_USE_SOCKET_VIEWER)
103         case 3:
104           if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
105           break;
106 #endif
107 #if defined(PETSC_HAVE_MATLAB_ENGINE)
108         case 4:
109           if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
110           break;
111 #endif
112 #if defined(PETSC_HAVE_SAWS)
113         case 5:
114           if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
115           break;
116 #endif
117 #if defined(PETSC_HAVE_HDF5)
118         case 7:
119           if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
120           break;
121 #endif
122         default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
123         }
124         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
125       } else {
126         if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
127           ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
128           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
129         } else {
130           PetscFileMode fmode;
131           ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
132           ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
133           fmode = FILE_MODE_WRITE;
134           if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
135             ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
136             if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
137           }
138           ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
139           ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
140           ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
141         }
142       }
143       if (loc2_fmt && *loc2_fmt) {
144         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr);
145         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr);
146       }
147       ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
148       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
149     }
150   }
151   PetscFunctionReturn(0);
152 }
153 
154 #undef __FUNCT__
155 #define __FUNCT__ "PetscViewerCreate"
156 /*@
157    PetscViewerCreate - Creates a viewing context
158 
159    Collective on MPI_Comm
160 
161    Input Parameter:
162 .  comm - MPI communicator
163 
164    Output Parameter:
165 .  inviewer - location to put the PetscViewer context
166 
167    Level: advanced
168 
169    Concepts: graphics^creating PetscViewer
170    Concepts: file input/output^creating PetscViewer
171    Concepts: sockets^creating PetscViewer
172 
173 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
174 
175 @*/
176 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
177 {
178   PetscViewer    viewer;
179   PetscErrorCode ierr;
180 
181   PetscFunctionBegin;
182   *inviewer = 0;
183   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
184   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);CHKERRQ(ierr);
185   *inviewer    = viewer;
186   viewer->data = 0;
187   PetscFunctionReturn(0);
188 }
189 
190 #undef __FUNCT__
191 #define __FUNCT__ "PetscViewerSetType"
192 /*@C
193    PetscViewerSetType - Builds PetscViewer for a particular implementation.
194 
195    Collective on PetscViewer
196 
197    Input Parameter:
198 +  viewer      - the PetscViewer context
199 -  type        - for example, PETSCVIEWERASCII
200 
201    Options Database Command:
202 .  -draw_type  <type> - Sets the type; use -help for a list
203     of available methods (for instance, ascii)
204 
205    Level: advanced
206 
207    Notes:
208    See "include/petscviewer.h" for available methods (for instance,
209    PETSCVIEWERSOCKET)
210 
211 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat()
212 @*/
213 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
214 {
215   PetscErrorCode ierr,(*r)(PetscViewer);
216   PetscBool      match;
217 
218   PetscFunctionBegin;
219   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
220   PetscValidCharPointer(type,2);
221   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
222   if (match) PetscFunctionReturn(0);
223 
224   /* cleanup any old type that may be there */
225   if (viewer->data) {
226     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
227 
228     viewer->ops->destroy = NULL;
229     viewer->data         = 0;
230   }
231   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
232 
233   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
234   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
235 
236   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
237   ierr = (*r)(viewer);CHKERRQ(ierr);
238   PetscFunctionReturn(0);
239 }
240 
241 #undef __FUNCT__
242 #define __FUNCT__ "PetscViewerRegister"
243 /*@C
244    PetscViewerRegister - Adds a viewer
245 
246    Not Collective
247 
248    Input Parameters:
249 +  name_solver - name of a new user-defined viewer
250 -  routine_create - routine to create method context
251 
252    Level: developer
253    Notes:
254    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
255 
256    Sample usage:
257 .vb
258    PetscViewerRegister("my_viewer_type",MyViewerCreate);
259 .ve
260 
261    Then, your solver can be chosen with the procedural interface via
262 $     PetscViewerSetType(viewer,"my_viewer_type")
263    or at runtime via the option
264 $     -viewer_type my_viewer_type
265 
266   Concepts: registering^Viewers
267 
268 .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
269  @*/
270 PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
271 {
272   PetscErrorCode ierr;
273 
274   PetscFunctionBegin;
275   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
276   PetscFunctionReturn(0);
277 }
278 
279 #undef __FUNCT__
280 #define __FUNCT__ "PetscViewerSetFromOptions"
281 /*@C
282    PetscViewerSetFromOptions - Sets the graphics type from the options database.
283       Defaults to a PETSc X windows graphics.
284 
285    Collective on PetscViewer
286 
287    Input Parameter:
288 .     PetscViewer - the graphics context
289 
290    Level: intermediate
291 
292    Notes:
293     Must be called after PetscViewerCreate() before the PetscViewer is used.
294 
295   Concepts: PetscViewer^setting options
296 
297 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
298 
299 @*/
300 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
301 {
302   PetscErrorCode    ierr;
303   char              vtype[256];
304   PetscBool         flg;
305 
306   PetscFunctionBegin;
307   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
308 
309   if (!PetscViewerList) {
310     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
311   }
312   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
313   ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
314   if (flg) {
315     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
316   }
317   /* type has not been set? */
318   if (!((PetscObject)viewer)->type_name) {
319     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
320   }
321   if (viewer->ops->setfromoptions) {
322     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
323   }
324 
325   /* process any options handlers added with PetscObjectAddOptionsHandler() */
326   ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr);
327   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
328   ierr = PetscOptionsEnd();CHKERRQ(ierr);
329   PetscFunctionReturn(0);
330 }
331