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