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