xref: /petsc/src/sys/classes/viewer/interface/view.c (revision e6e75211d226c622f451867f53ce5d558649ff4f)
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, "-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, "-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__ "PetscViewerGetType"
114 /*@C
115    PetscViewerGetType - Returns the type of a PetscViewer.
116 
117    Not Collective
118 
119    Input Parameter:
120 .   viewer - the PetscViewer
121 
122    Output Parameter:
123 .  type - PetscViewer type (see below)
124 
125    Available Types Include:
126 .  PETSCVIEWERSOCKET - Socket PetscViewer
127 .  PETSCVIEWERASCII - ASCII PetscViewer
128 .  PETSCVIEWERBINARY - binary file PetscViewer
129 .  PETSCVIEWERSTRING - string PetscViewer
130 .  PETSCVIEWERDRAW - drawing PetscViewer
131 
132    Level: intermediate
133 
134    Note:
135    See include/petscviewer.h for a complete list of PetscViewers.
136 
137    PetscViewerType is actually a string
138 
139 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
140 
141 @*/
142 PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
143 {
144   PetscFunctionBegin;
145   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
146   PetscValidPointer(type,2);
147   *type = ((PetscObject)viewer)->type_name;
148   PetscFunctionReturn(0);
149 }
150 
151 #undef __FUNCT__
152 #define __FUNCT__ "PetscViewerSetOptionsPrefix"
153 /*@C
154    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
155    PetscViewer options in the database.
156 
157    Logically Collective on PetscViewer
158 
159    Input Parameter:
160 +  viewer - the PetscViewer context
161 -  prefix - the prefix to prepend to all option names
162 
163    Notes:
164    A hyphen (-) must NOT be given at the beginning of the prefix name.
165    The first character of all runtime options is AUTOMATICALLY the hyphen.
166 
167    Level: advanced
168 
169 .keywords: PetscViewer, set, options, prefix, database
170 
171 .seealso: PetscViewerSetFromOptions()
172 @*/
173 PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
174 {
175   PetscErrorCode ierr;
176 
177   PetscFunctionBegin;
178   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
179   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
180   PetscFunctionReturn(0);
181 }
182 
183 #undef __FUNCT__
184 #define __FUNCT__ "PetscViewerAppendOptionsPrefix"
185 /*@C
186    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
187    PetscViewer options in the database.
188 
189    Logically Collective on PetscViewer
190 
191    Input Parameters:
192 +  viewer - the PetscViewer context
193 -  prefix - the prefix to prepend to all option names
194 
195    Notes:
196    A hyphen (-) must NOT be given at the beginning of the prefix name.
197    The first character of all runtime options is AUTOMATICALLY the hyphen.
198 
199    Level: advanced
200 
201 .keywords: PetscViewer, append, options, prefix, database
202 
203 .seealso: PetscViewerGetOptionsPrefix()
204 @*/
205 PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
206 {
207   PetscErrorCode ierr;
208 
209   PetscFunctionBegin;
210   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
211   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
212   PetscFunctionReturn(0);
213 }
214 
215 #undef __FUNCT__
216 #define __FUNCT__ "PetscViewerGetOptionsPrefix"
217 /*@C
218    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
219    PetscViewer options in the database.
220 
221    Not Collective
222 
223    Input Parameter:
224 .  viewer - the PetscViewer context
225 
226    Output Parameter:
227 .  prefix - pointer to the prefix string used
228 
229    Notes: On the fortran side, the user should pass in a string 'prefix' of
230    sufficient length to hold the prefix.
231 
232    Level: advanced
233 
234 .keywords: PetscViewer, get, options, prefix, database
235 
236 .seealso: PetscViewerAppendOptionsPrefix()
237 @*/
238 PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
239 {
240   PetscErrorCode ierr;
241 
242   PetscFunctionBegin;
243   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
244   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
245   PetscFunctionReturn(0);
246 }
247 
248 #undef __FUNCT__
249 #define __FUNCT__ "PetscViewerSetUp"
250 /*@
251    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
252 
253    Collective on PetscViewer
254 
255    Input Parameters:
256 .  viewer - the PetscViewer context
257 
258    Notes:
259    For basic use of the PetscViewer classes the user need not explicitly call
260    PetscViewerSetUp(), since these actions will happen automatically.
261 
262    Level: advanced
263 
264 .keywords: PetscViewer, setup
265 
266 .seealso: PetscViewerCreate(), PetscViewerDestroy()
267 @*/
268 PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
269 {
270   PetscErrorCode ierr;
271 
272   PetscFunctionBegin;
273   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
274   if (viewer->setupcalled) PetscFunctionReturn(0);
275   if (viewer->ops->setup) {
276     ierr = (*viewer->ops->setup)(viewer);CHKERRQ(ierr);
277   }
278   viewer->setupcalled = PETSC_TRUE;
279   PetscFunctionReturn(0);
280 }
281 
282 #undef __FUNCT__
283 #define __FUNCT__ "PetscViewerView"
284 /*@C
285    PetscViewerView - Visualizes a viewer object.
286 
287    Collective on PetscViewer
288 
289    Input Parameters:
290 +  v - the viewer
291 -  viewer - visualization context
292 
293   Notes:
294   The available visualization contexts include
295 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
296 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
297         output where only the first processor opens
298         the file.  All other processors send their
299         data to the first processor to print.
300 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
301 
302    Level: beginner
303 
304 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
305           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
306 @*/
307 PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
308 {
309   PetscErrorCode    ierr;
310   PetscBool         iascii;
311   PetscViewerFormat format;
312 #if defined(PETSC_HAVE_SAWS)
313   PetscBool         issaws;
314 #endif
315 
316   PetscFunctionBegin;
317   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
318   PetscValidType(v,1);
319   if (!viewer) {
320     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
321   }
322   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
323   PetscCheckSameComm(v,1,viewer,2);
324 
325   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
326 #if defined(PETSC_HAVE_SAWS)
327   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr);
328 #endif
329   if (iascii) {
330     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
331     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr);
332     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
333       if (v->format) {
334         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
335       }
336       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
337       if (v->ops->view) {
338         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
339       }
340       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
341     }
342 #if defined(PETSC_HAVE_SAWS)
343   } else if (issaws) {
344     if (!((PetscObject)v)->amsmem) {
345       ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr);
346       if (v->ops->view) {
347         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
348       }
349     }
350 #endif
351   }
352   PetscFunctionReturn(0);
353 }
354 
355 #undef __FUNCT__
356 #define __FUNCT__ "PetscViewerRead"
357 /*@C
358    PetscViewerRead - Reads data from a PetscViewer
359 
360    Collective on MPI_Comm
361 
362    Input Parameters:
363 +  viewer   - The viewer
364 .  data     - Location to write the data
365 .  num      - Number of items of data to read
366 -  datatype - Type of data to read
367 
368    Output Parameters:
369 .  count - number of items of data actually read, or NULL
370 
371    Level: beginner
372 
373    Concepts: binary files, ascii files
374 
375 .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
376           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
377           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
378 @*/
379 PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
380 {
381   PetscErrorCode ierr;
382 
383   PetscFunctionBegin;
384   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
385   if (dtype == PETSC_STRING) {
386     PetscInt c, i = 0, cnt;
387     char *s = (char *)data;
388     for (c = 0; c < num; c++) {
389       /* Skip leading whitespaces */
390       do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
391       while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
392       i++;
393       /* Read strings one char at a time */
394       do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR);CHKERRQ(ierr); if (count && !cnt) break;}
395       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');
396       /* Terminate final string */
397       if (c == num-1) s[i-1] = '\0';
398     }
399     if (count) *count = c;
400     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
401   } else {
402     ierr = (*viewer->ops->read)(viewer, data, num, count, dtype);CHKERRQ(ierr);
403   }
404   PetscFunctionReturn(0);
405 }
406