xref: /petsc/src/sys/classes/viewer/interface/view.c (revision e611a964e9853b74d61a56642fe9d06a6e51780f)
1 
2 #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3 
4 PetscClassId PETSC_VIEWER_CLASSID;
5 
6 static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
7 #undef __FUNCT__
8 #define __FUNCT__ "PetscViewerFinalizePackage"
9 /*@C
10   PetscViewerFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
11   called from PetscFinalize().
12 
13   Level: developer
14 
15 .keywords: Petsc, destroy, package, mathematica
16 .seealso: PetscFinalize()
17 @*/
18 PetscErrorCode  PetscViewerFinalizePackage(void)
19 {
20   PetscErrorCode ierr;
21 
22   PetscFunctionBegin;
23   ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr);
24   PetscViewerPackageInitialized = PETSC_FALSE;
25   PetscViewerRegisterAllCalled  = PETSC_FALSE;
26   PetscFunctionReturn(0);
27 }
28 
29 #undef __FUNCT__
30 #define __FUNCT__ "PetscViewerInitializePackage"
31 /*@C
32   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
33 
34   Level: developer
35 
36 .keywords: Petsc, initialize, package
37 .seealso: PetscInitialize()
38 @*/
39 PetscErrorCode  PetscViewerInitializePackage(void)
40 {
41   char           logList[256];
42   char           *className;
43   PetscBool      opt;
44   PetscErrorCode ierr;
45 
46   PetscFunctionBegin;
47   if (PetscViewerPackageInitialized) PetscFunctionReturn(0);
48   PetscViewerPackageInitialized = PETSC_TRUE;
49   /* Register Classes */
50   ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr);
51 
52   /* Register Constructors */
53   ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
54 
55   /* Process info exclusions */
56   ierr = PetscOptionsGetString(NULL,NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr);
57   if (opt) {
58     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
59     if (className) {
60       ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr);
61     }
62   }
63   /* Process summary exclusions */
64   ierr = PetscOptionsGetString(NULL,NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr);
65   if (opt) {
66     ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr);
67     if (className) {
68       ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr);
69     }
70   }
71 #if defined(PETSC_HAVE_MATHEMATICA)
72   ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr);
73 #endif
74   ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr);
75   PetscFunctionReturn(0);
76 }
77 
78 #undef __FUNCT__
79 #define __FUNCT__ "PetscViewerDestroy"
80 /*@
81    PetscViewerDestroy - Destroys a PetscViewer.
82 
83    Collective on PetscViewer
84 
85    Input Parameters:
86 .  viewer - the PetscViewer to be destroyed.
87 
88    Level: beginner
89 
90 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
91 
92 @*/
93 PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
94 {
95   PetscErrorCode ierr;
96 
97   PetscFunctionBegin;
98   if (!*viewer) PetscFunctionReturn(0);
99   PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1);
100 
101   ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr);
102   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);}
103 
104   ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr);
105   if ((*viewer)->ops->destroy) {
106     ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr);
107   }
108   ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr);
109   PetscFunctionReturn(0);
110 }
111 
112 #undef __FUNCT__
113 #define __FUNCT__ "PetscViewerAndFormatCreate"
114 /*@C
115    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
116 
117    Collective on PetscViewer
118 
119    Input Parameters:
120 +  viewer - the viewer
121 -  format - the format
122 
123    Output Parameter:
124 .   vf - viewer and format object
125 
126    Notes: This increases the reference count of the viewer so you can destroy the viewer object after this call
127    Level: developer
128 
129    This is used as the context variable for many of the TS, SNES, and KSP monitor functions
130 
131 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
132 
133 @*/
134 PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
135 {
136   PetscErrorCode ierr;
137 
138   PetscFunctionBegin;
139   ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr);
140   ierr = PetscNew(vf);CHKERRQ(ierr);
141   (*vf)->viewer = viewer;
142   (*vf)->format = format;
143   PetscFunctionReturn(0);
144 }
145 
146 
147 #undef __FUNCT__
148 #define __FUNCT__ "PetscViewerAndFormatDestroy"
149 /*@C
150    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
151 
152    Collective on PetscViewer
153 
154    Input Parameters:
155 .  viewer - the PetscViewerAndFormat to be destroyed.
156 
157    Level: developer
158 
159 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
160 
161 @*/
162 PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
163 {
164   PetscErrorCode ierr;
165 
166   PetscFunctionBegin;
167   ierr = PetscViewerDestroy(&(*vf)->viewer);CHKERRQ(ierr);
168   ierr = PetscFree(*vf);CHKERRQ(ierr);
169   PetscFunctionReturn(0);
170 }
171 
172 #undef __FUNCT__
173 #define __FUNCT__ "PetscViewerGetType"
174 /*@C
175    PetscViewerGetType - Returns the type of a PetscViewer.
176 
177    Not Collective
178 
179    Input Parameter:
180 .   viewer - the PetscViewer
181 
182    Output Parameter:
183 .  type - PetscViewer type (see below)
184 
185    Available Types Include:
186 .  PETSCVIEWERSOCKET - Socket PetscViewer
187 .  PETSCVIEWERASCII - ASCII PetscViewer
188 .  PETSCVIEWERBINARY - binary file PetscViewer
189 .  PETSCVIEWERSTRING - string PetscViewer
190 .  PETSCVIEWERDRAW - drawing PetscViewer
191 
192    Level: intermediate
193 
194    Note:
195    See include/petscviewer.h for a complete list of PetscViewers.
196 
197    PetscViewerType is actually a string
198 
199 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
200 
201 @*/
202 PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
203 {
204   PetscFunctionBegin;
205   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
206   PetscValidPointer(type,2);
207   *type = ((PetscObject)viewer)->type_name;
208   PetscFunctionReturn(0);
209 }
210 
211 #undef __FUNCT__
212 #define __FUNCT__ "PetscViewerSetOptionsPrefix"
213 /*@C
214    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
215    PetscViewer options in the database.
216 
217    Logically Collective on PetscViewer
218 
219    Input Parameter:
220 +  viewer - the PetscViewer context
221 -  prefix - the prefix to prepend to all option names
222 
223    Notes:
224    A hyphen (-) must NOT be given at the beginning of the prefix name.
225    The first character of all runtime options is AUTOMATICALLY the hyphen.
226 
227    Level: advanced
228 
229 .keywords: PetscViewer, set, options, prefix, database
230 
231 .seealso: PetscViewerSetFromOptions()
232 @*/
233 PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
234 {
235   PetscErrorCode ierr;
236 
237   PetscFunctionBegin;
238   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
239   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
240   PetscFunctionReturn(0);
241 }
242 
243 #undef __FUNCT__
244 #define __FUNCT__ "PetscViewerAppendOptionsPrefix"
245 /*@C
246    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
247    PetscViewer options in the database.
248 
249    Logically Collective on PetscViewer
250 
251    Input Parameters:
252 +  viewer - the PetscViewer context
253 -  prefix - the prefix to prepend to all option names
254 
255    Notes:
256    A hyphen (-) must NOT be given at the beginning of the prefix name.
257    The first character of all runtime options is AUTOMATICALLY the hyphen.
258 
259    Level: advanced
260 
261 .keywords: PetscViewer, append, options, prefix, database
262 
263 .seealso: PetscViewerGetOptionsPrefix()
264 @*/
265 PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
266 {
267   PetscErrorCode ierr;
268 
269   PetscFunctionBegin;
270   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
271   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
272   PetscFunctionReturn(0);
273 }
274 
275 #undef __FUNCT__
276 #define __FUNCT__ "PetscViewerGetOptionsPrefix"
277 /*@C
278    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
279    PetscViewer options in the database.
280 
281    Not Collective
282 
283    Input Parameter:
284 .  viewer - the PetscViewer context
285 
286    Output Parameter:
287 .  prefix - pointer to the prefix string used
288 
289    Notes: On the fortran side, the user should pass in a string 'prefix' of
290    sufficient length to hold the prefix.
291 
292    Level: advanced
293 
294 .keywords: PetscViewer, get, options, prefix, database
295 
296 .seealso: PetscViewerAppendOptionsPrefix()
297 @*/
298 PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
299 {
300   PetscErrorCode ierr;
301 
302   PetscFunctionBegin;
303   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
304   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
305   PetscFunctionReturn(0);
306 }
307 
308 #undef __FUNCT__
309 #define __FUNCT__ "PetscViewerSetUp"
310 /*@
311    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
312 
313    Collective on PetscViewer
314 
315    Input Parameters:
316 .  viewer - the PetscViewer context
317 
318    Notes:
319    For basic use of the PetscViewer classes the user need not explicitly call
320    PetscViewerSetUp(), since these actions will happen automatically.
321 
322    Level: advanced
323 
324 .keywords: PetscViewer, setup
325 
326 .seealso: PetscViewerCreate(), PetscViewerDestroy()
327 @*/
328 PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
329 {
330   PetscErrorCode ierr;
331 
332   PetscFunctionBegin;
333   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
334   if (viewer->setupcalled) PetscFunctionReturn(0);
335   if (viewer->ops->setup) {
336     ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr);
337   }
338   viewer->setupcalled = PETSC_TRUE;
339   PetscFunctionReturn(0);
340 }
341 
342 #undef __FUNCT__
343 #define __FUNCT__ "PetscViewerView"
344 /*@C
345    PetscViewerView - Visualizes a viewer object.
346 
347    Collective on PetscViewer
348 
349    Input Parameters:
350 +  v - the viewer
351 -  viewer - visualization context
352 
353   Notes:
354   The available visualization contexts include
355 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
356 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
357         output where only the first processor opens
358         the file.  All other processors send their
359         data to the first processor to print.
360 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
361 
362    Level: beginner
363 
364 .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
365           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
366 @*/
367 PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
368 {
369   PetscErrorCode    ierr;
370   PetscBool         iascii;
371   PetscViewerFormat format;
372 #if defined(PETSC_HAVE_SAWS)
373   PetscBool         issaws;
374 #endif
375 
376   PetscFunctionBegin;
377   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
378   PetscValidType(v,1);
379   if (!viewer) {
380     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
381   }
382   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
383   PetscCheckSameComm(v,1,viewer,2);
384 
385   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
386 #if defined(PETSC_HAVE_SAWS)
387   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
388 #endif
389   if (iascii) {
390     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
391     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr);
392     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
393       if (v->format) {
394         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
395       }
396       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
397       if (v->ops->view) {
398         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
399       }
400       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
401     }
402 #if defined(PETSC_HAVE_SAWS)
403   } else if (issaws) {
404     if (!((PetscObject)v)->amsmem) {
405       ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr);
406       if (v->ops->view) {
407         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
408       }
409     }
410 #endif
411   }
412   PetscFunctionReturn(0);
413 }
414 
415 #undef __FUNCT__
416 #define __FUNCT__ "PetscViewerRead"
417 /*@C
418    PetscViewerRead - Reads data from a PetscViewer
419 
420    Collective on MPI_Comm
421 
422    Input Parameters:
423 +  viewer   - The viewer
424 .  data     - Location to write the data
425 .  num      - Number of items of data to read
426 -  datatype - Type of data to read
427 
428    Output Parameters:
429 .  count - number of items of data actually read, or NULL
430 
431    Level: beginner
432 
433    Concepts: binary files, ascii files
434 
435 .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
436           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
437           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
438 @*/
439 PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
440 {
441   PetscErrorCode ierr;
442 
443   PetscFunctionBegin;
444   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
445   if (dtype == PETSC_STRING) {
446     PetscInt c, i = 0, cnt;
447     char *s = (char *)data;
448     for (c = 0; c < num; c++) {
449       /* Skip leading whitespaces */
450       do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
451       while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
452       i++;
453       /* Read strings one char at a time */
454       do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
455       while (s[i-1]!='\n' && s[i-1]!='\t' && s[i-1]!=' ' && s[i-1]!='\0' && s[i-1]!='\v' && s[i-1]!='\f' && s[i-1]!='\r');
456       /* Terminate final string */
457       if (c == num-1) s[i-1] = '\0';
458     }
459     if (count) *count = c;
460     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
461   } else {
462     ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr);
463   }
464   PetscFunctionReturn(0);
465 }
466