xref: /petsc/src/sys/classes/viewer/interface/view.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
1 
2 #include <petsc-private/viewerimpl.h>  /*I "petscsys.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   PetscFunctionBegin;
21   PetscViewerPackageInitialized = PETSC_FALSE;
22   PetscViewerList               = 0;
23   PetscFunctionReturn(0);
24 }
25 
26 #undef __FUNCT__
27 #define __FUNCT__ "PetscViewerInitializePackage"
28 /*@C
29   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
30 
31   Input Parameter:
32   path - The dynamic library path, or NULL
33 
34   Level: developer
35 
36 .keywords: Petsc, initialize, package
37 .seealso: PetscInitialize()
38 @*/
39 PetscErrorCode  PetscViewerInitializePackage(const char path[])
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(path);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(NULL);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 = PetscObjectDepublish(*viewer);CHKERRQ(ierr);
105 
106   if ((*viewer)->ops->destroy) {
107     ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr);
108   }
109   ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr);
110   PetscFunctionReturn(0);
111 }
112 
113 #undef __FUNCT__
114 #define __FUNCT__ "PetscViewerGetType"
115 /*@C
116    PetscViewerGetType - Returns the type of a PetscViewer.
117 
118    Not Collective
119 
120    Input Parameter:
121 .   viewer - the PetscViewer
122 
123    Output Parameter:
124 .  type - PetscViewer type (see below)
125 
126    Available Types Include:
127 .  PETSCVIEWERSOCKET - Socket PetscViewer
128 .  PETSCVIEWERASCII - ASCII PetscViewer
129 .  PETSCVIEWERBINARY - binary file PetscViewer
130 .  PETSCVIEWERSTRING - string PetscViewer
131 .  PETSCVIEWERDRAW - drawing PetscViewer
132 
133    Level: intermediate
134 
135    Note:
136    See include/petscviewer.h for a complete list of PetscViewers.
137 
138    PetscViewerType is actually a string
139 
140 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
141 
142 @*/
143 PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
144 {
145   PetscFunctionBegin;
146   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
147   PetscValidPointer(type,2);
148   *type = ((PetscObject)viewer)->type_name;
149   PetscFunctionReturn(0);
150 }
151 
152 #undef __FUNCT__
153 #define __FUNCT__ "PetscViewerSetOptionsPrefix"
154 /*@C
155    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
156    PetscViewer options in the database.
157 
158    Logically Collective on PetscViewer
159 
160    Input Parameter:
161 +  viewer - the PetscViewer context
162 -  prefix - the prefix to prepend to all option names
163 
164    Notes:
165    A hyphen (-) must NOT be given at the beginning of the prefix name.
166    The first character of all runtime options is AUTOMATICALLY the hyphen.
167 
168    Level: advanced
169 
170 .keywords: PetscViewer, set, options, prefix, database
171 
172 .seealso: PetscViewerSetFromOptions()
173 @*/
174 PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
175 {
176   PetscErrorCode ierr;
177 
178   PetscFunctionBegin;
179   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
180   ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
181   PetscFunctionReturn(0);
182 }
183 
184 #undef __FUNCT__
185 #define __FUNCT__ "PetscViewerAppendOptionsPrefix"
186 /*@C
187    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
188    PetscViewer options in the database.
189 
190    Logically Collective on PetscViewer
191 
192    Input Parameters:
193 +  viewer - the PetscViewer context
194 -  prefix - the prefix to prepend to all option names
195 
196    Notes:
197    A hyphen (-) must NOT be given at the beginning of the prefix name.
198    The first character of all runtime options is AUTOMATICALLY the hyphen.
199 
200    Level: advanced
201 
202 .keywords: PetscViewer, append, options, prefix, database
203 
204 .seealso: PetscViewerGetOptionsPrefix()
205 @*/
206 PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
207 {
208   PetscErrorCode ierr;
209 
210   PetscFunctionBegin;
211   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
212   ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
213   PetscFunctionReturn(0);
214 }
215 
216 #undef __FUNCT__
217 #define __FUNCT__ "PetscViewerGetOptionsPrefix"
218 /*@C
219    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
220    PetscViewer options in the database.
221 
222    Not Collective
223 
224    Input Parameter:
225 .  viewer - the PetscViewer context
226 
227    Output Parameter:
228 .  prefix - pointer to the prefix string used
229 
230    Notes: On the fortran side, the user should pass in a string 'prefix' of
231    sufficient length to hold the prefix.
232 
233    Level: advanced
234 
235 .keywords: PetscViewer, get, options, prefix, database
236 
237 .seealso: PetscViewerAppendOptionsPrefix()
238 @*/
239 PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
240 {
241   PetscErrorCode ierr;
242 
243   PetscFunctionBegin;
244   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
245   ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr);
246   PetscFunctionReturn(0);
247 }
248 
249 #undef __FUNCT__
250 #define __FUNCT__ "PetscViewerSetUp"
251 /*@
252    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
253 
254    Collective on PetscViewer
255 
256    Input Parameters:
257 .  viewer - the PetscViewer context
258 
259    Notes:
260    For basic use of the PetscViewer classes the user need not explicitly call
261    PetscViewerSetUp(), since these actions will happen automatically.
262 
263    Level: advanced
264 
265 .keywords: PetscViewer, setup
266 
267 .seealso: PetscViewerCreate(), PetscViewerDestroy()
268 @*/
269 PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
270 {
271   PetscFunctionBegin;
272   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
273   PetscFunctionReturn(0);
274 }
275 
276 #undef __FUNCT__
277 #define __FUNCT__ "PetscViewerView"
278 /*@C
279    PetscViewerView - Visualizes a viewer object.
280 
281    Collective on PetscViewer
282 
283    Input Parameters:
284 +  v - the viewer
285 -  viewer - visualization context
286 
287   Notes:
288   The available visualization contexts include
289 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
290 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
291         output where only the first processor opens
292         the file.  All other processors send their
293         data to the first processor to print.
294 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
295 
296    Level: beginner
297 
298 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
299           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
300 @*/
301 PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
302 {
303   PetscErrorCode    ierr;
304   PetscBool         iascii;
305   PetscViewerFormat format;
306 
307   PetscFunctionBegin;
308   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
309   PetscValidType(v,1);
310   if (!viewer) {
311     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
312   }
313   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
314   PetscCheckSameComm(v,1,viewer,2);
315 
316   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
317   if (iascii) {
318     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
319     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
320       ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer,"PetscViewer Object");CHKERRQ(ierr);
321       if (v->format) {
322         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
323       }
324       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
325       if (v->ops->view) {
326         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
327       }
328       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
329     }
330   }
331   PetscFunctionReturn(0);
332 }
333