xref: /petsc/src/sys/classes/viewer/interface/view.c (revision ec7429eab6e231810adac0f010b269bd5bae4496)
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   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 = PetscObjectAMSUnPublish((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   PetscFunctionBegin;
271   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
272   PetscFunctionReturn(0);
273 }
274 
275 #undef __FUNCT__
276 #define __FUNCT__ "PetscViewerView"
277 /*@C
278    PetscViewerView - Visualizes a viewer object.
279 
280    Collective on PetscViewer
281 
282    Input Parameters:
283 +  v - the viewer
284 -  viewer - visualization context
285 
286   Notes:
287   The available visualization contexts include
288 +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
289 .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
290         output where only the first processor opens
291         the file.  All other processors send their
292         data to the first processor to print.
293 -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
294 
295    Level: beginner
296 
297 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
298           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
299 @*/
300 PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
301 {
302   PetscErrorCode    ierr;
303   PetscBool         iascii;
304   PetscViewerFormat format;
305 
306   PetscFunctionBegin;
307   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
308   PetscValidType(v,1);
309   if (!viewer) {
310     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr);
311   }
312   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
313   PetscCheckSameComm(v,1,viewer,2);
314 
315   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
316   if (iascii) {
317     ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
318     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
319       ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer,"PetscViewer Object");CHKERRQ(ierr);
320       if (v->format) {
321         ierr = PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr);
322       }
323       ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr);
324       if (v->ops->view) {
325         ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr);
326       }
327       ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr);
328     }
329   }
330   PetscFunctionReturn(0);
331 }
332