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