Actual source code: viewreg.c
petsc-3.4.5 2014-06-29
2: #include <petsc-private/viewerimpl.h> /*I "petscviewer.h" I*/
3: #if defined(PETSC_HAVE_AMS)
4: #include <petscviewerams.h>
5: #endif
7: PetscFunctionList PetscViewerList = 0;
11: /*@C
12: PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
14: Collective on MPI_Comm
16: Input Parameters:
17: + comm - the communicator to own the viewer
18: . pre - the string to prepend to the name or NULL
19: - name - the option one is seeking
21: Output Parameter:
22: + viewer - the viewer
23: . format - the PetscViewerFormat requested by the user
24: - set - PETSC_TRUE if found, else PETSC_FALSE
26: Level: intermediate
28: Notes: If no value is provided ascii:stdout is used
29: $ ascii[:[filename][:[format][:append]]] defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab, for example ascii::ascii_info prints just the info
30: $ about the object to standard out - unless :append is given filename opens in write mode
31: $ binary[:[filename][:[format][:append]]] defaults to binaryoutput
32: $ draw
33: $ socket[:port] defaults to the standard output port
34: $ ams[:communicatorname] publishes object to the AMS (Argonne Memory Snooper)
36: Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
38: .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
39: PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
40: PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
41: PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
42: PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
43: PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
44: PetscOptionsList(), PetscOptionsEList()
45: @*/
46: PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool *set)
47: {
48: char *value;
50: PetscBool flag;
55: if (format) *format = PETSC_VIEWER_DEFAULT;
56: if (set) *set = PETSC_FALSE;
57: PetscOptionsFindPair_Private(pre,name,&value,&flag);
58: if (flag) {
59: if (set) *set = PETSC_TRUE;
60: if (!value) {
61: PetscViewerASCIIGetStdout(comm,viewer);
62: PetscObjectReference((PetscObject)*viewer);
63: } else {
64: char *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
65: PetscInt cnt;
66: const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERAMS,PETSCVIEWERVTK,0};
68: PetscStrallocpy(value,&loc0_vtype);
69: PetscStrchr(loc0_vtype,':',&loc1_fname);
70: if (loc1_fname) {
71: *loc1_fname++ = 0;
72: PetscStrchr(loc1_fname,':',&loc2_fmt);
73: }
74: if (loc2_fmt) {
75: *loc2_fmt++ = 0;
76: PetscStrchr(loc2_fmt,':',&loc3_fmode);
77: }
78: if (loc3_fmode) *loc3_fmode++ = 0;
79: PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);
80: if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
81: if (!loc1_fname) {
82: switch (cnt) {
83: case 0:
84: PetscViewerASCIIGetStdout(comm,viewer);
85: break;
86: case 1:
87: if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
88: break;
89: case 2:
90: if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
91: break;
92: #if defined(PETSC_USE_SOCKET_VIEWER)
93: case 3:
94: if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
95: break;
96: #endif
97: #if defined(PETSC_HAVE_MATLAB_ENGINE)
98: case 4:
99: if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
100: break;
101: #endif
102: #if defined(PETSC_HAVE_AMS)
103: case 5:
104: if (!(*viewer = PETSC_VIEWER_AMS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
105: break;
106: #endif
107: default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
108: }
109: PetscObjectReference((PetscObject)*viewer);
110: } else {
111: if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
112: PetscViewerASCIIGetStdout(comm,viewer);
113: PetscObjectReference((PetscObject)*viewer);
114: } else {
115: PetscFileMode fmode;
116: PetscViewerCreate(comm,viewer);
117: PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");
118: #if defined(PETSC_HAVE_AMS)
119: PetscViewerAMSSetCommName(*viewer,loc1_fname);
120: #endif
121: fmode = FILE_MODE_WRITE;
122: if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
123: PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);
124: if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
125: }
126: PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
127: PetscViewerFileSetName(*viewer,loc1_fname);
128: }
129: }
130: if (loc2_fmt && *loc2_fmt) {
131: PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
132: if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
133: }
134: PetscViewerSetUp(*viewer);
135: PetscFree(loc0_vtype);
136: }
137: }
138: return(0);
139: }
143: /*@
144: PetscViewerCreate - Creates a viewing context
146: Collective on MPI_Comm
148: Input Parameter:
149: . comm - MPI communicator
151: Output Parameter:
152: . inviewer - location to put the PetscViewer context
154: Level: advanced
156: Concepts: graphics^creating PetscViewer
157: Concepts: file input/output^creating PetscViewer
158: Concepts: sockets^creating PetscViewer
160: .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
162: @*/
163: PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
164: {
165: PetscViewer viewer;
169: *inviewer = 0;
170: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
171: PetscViewerInitializePackage();
172: #endif
173: PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);
174: *inviewer = viewer;
175: viewer->data = 0;
176: return(0);
177: }
181: /*@C
182: PetscViewerSetType - Builds PetscViewer for a particular implementation.
184: Collective on PetscViewer
186: Input Parameter:
187: + viewer - the PetscViewer context
188: - type - for example, PETSCVIEWERASCII
190: Options Database Command:
191: . -draw_type <type> - Sets the type; use -help for a list
192: of available methods (for instance, ascii)
194: Level: advanced
196: Notes:
197: See "include/petscviewer.h" for available methods (for instance,
198: PETSCVIEWERSOCKET)
200: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat()
201: @*/
202: PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
203: {
204: PetscErrorCode ierr,(*r)(PetscViewer);
205: PetscBool match;
210: PetscObjectTypeCompare((PetscObject)viewer,type,&match);
211: if (match) return(0);
213: /* cleanup any old type that may be there */
214: if (viewer->data) {
215: (*viewer->ops->destroy)(viewer);
217: viewer->ops->destroy = NULL;
218: viewer->data = 0;
219: }
220: PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));
222: PetscFunctionListFind(PetscViewerList,type,&r);
223: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
225: PetscObjectChangeTypeName((PetscObject)viewer,type);
226: (*r)(viewer);
227: return(0);
228: }
232: /*@C
233: PetscViewerRegister - Adds a viewer
235: Not Collective
237: Input Parameters:
238: + name_solver - name of a new user-defined viewer
239: - routine_create - routine to create method context
241: Level: developer
242: Notes:
243: PetscViewerRegister() may be called multiple times to add several user-defined viewers.
245: Sample usage:
246: .vb
247: PetscViewerRegister("my_viewer_type",MyViewerCreate);
248: .ve
250: Then, your solver can be chosen with the procedural interface via
251: $ PetscViewerSetType(viewer,"my_viewer_type")
252: or at runtime via the option
253: $ -viewer_type my_viewer_type
255: Concepts: registering^Viewers
257: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
258: @*/
259: PetscErrorCode PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
260: {
264: PetscFunctionListAdd(&PetscViewerList,sname,function);
265: return(0);
266: }
270: /*@C
271: PetscViewerSetFromOptions - Sets the graphics type from the options database.
272: Defaults to a PETSc X windows graphics.
274: Collective on PetscViewer
276: Input Parameter:
277: . PetscViewer - the graphics context
279: Level: intermediate
281: Notes:
282: Must be called after PetscViewerCreate() before the PetscViewer is used.
284: Concepts: PetscViewer^setting options
286: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
288: @*/
289: PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer)
290: {
292: char vtype[256];
293: PetscBool flg;
298: if (!PetscViewerList) {
299: PetscViewerRegisterAll();
300: }
301: PetscObjectOptionsBegin((PetscObject)viewer);
302: PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
303: if (flg) {
304: PetscViewerSetType(viewer,vtype);
305: }
306: /* type has not been set? */
307: if (!((PetscObject)viewer)->type_name) {
308: PetscViewerSetType(viewer,PETSCVIEWERASCII);
309: }
310: if (viewer->ops->setfromoptions) {
311: (*viewer->ops->setfromoptions)(viewer);
312: }
314: /* process any options handlers added with PetscObjectAddOptionsHandler() */
315: PetscObjectProcessOptionsHandlers((PetscObject)viewer);
316: PetscOptionsEnd();
317: return(0);
318: }