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