1 2 #include <petsc-private/viewerimpl.h> /*I "petscsys.h" I*/ 3 4 PetscFList PetscViewerList = 0; 5 6 PetscErrorCode PetscOptionsFindPair_Private(const char[],const char[],char *[],PetscBool*); 7 #undef __FUNCT__ 8 #define __FUNCT__ "PetscOptionsGetViewer" 9 /*@C 10 PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user 11 12 Collective on MPI_Comm 13 14 Input Parameters: 15 + comm - the communicator to own the viewer 16 . pre - the string to prepend to the name or PETSC_NULL 17 - name - the option one is seeking 18 19 Output Parameter: 20 + viewer - the viewer 21 - set - PETSC_TRUE if found, else PETSC_FALSE 22 23 Level: intermediate 24 25 Notes: If no value is provided ascii:stdout is used 26 $ ascii[:[filename][:format]] defaults to stdout - format can be one of info, info_detailed, or matlab, for example ascii::info prints just the info 27 $ about the object to standard out 28 $ binary[:filename] defaults to binaryoutput 29 $ draw 30 $ socket[:port] defaults to the standard output port 31 32 Use PetscOptionsRestoreViewer() after using the viewer, otherwise a memory leak may occur 33 34 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 35 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 36 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 37 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 38 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 39 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 40 PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer() 41 @*/ 42 PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscBool *set) 43 { 44 char *value; 45 PetscErrorCode ierr; 46 PetscBool flag; 47 48 PetscFunctionBegin; 49 PetscValidCharPointer(name,3); 50 51 if (set) *set = PETSC_FALSE; 52 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 53 if (flag) { 54 if (set) *set = PETSC_TRUE; 55 if (!value) { 56 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 57 } else { 58 char *cvalue,*loc,*loc2 = PETSC_NULL; 59 PetscInt cnt; 60 const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,0}; 61 62 ierr = PetscStrallocpy(value,&cvalue);CHKERRQ(ierr); 63 ierr = PetscStrchr(cvalue,':',&loc);CHKERRQ(ierr); 64 if (loc) {*loc = 0; loc++;} 65 ierr = PetscStrendswithwhich(*cvalue ? cvalue : "ascii",viewers,&cnt);CHKERRQ(ierr); 66 if (cnt == 4) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",cvalue); 67 if (!loc) { 68 if (cnt == 0) { 69 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 70 } 71 if (cnt == 1) { 72 *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr); 73 } 74 if (cnt == 2) { 75 *viewer = PETSC_VIEWER_DRAW_(comm);CHKERRQ(ierr); 76 } 77 if (cnt == 3) { 78 *viewer = PETSC_VIEWER_SOCKET_(comm);CHKERRQ(ierr); 79 } 80 } else { 81 ierr = PetscStrchr(loc,':',&loc2);CHKERRQ(ierr); 82 if (loc2) {*loc2 = 0; loc2++;} 83 if (loc2 && !*loc && (cnt == 0)) { /* ASCII format without file name */ 84 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 85 } else { 86 ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr); 87 ierr = PetscViewerSetType(*viewer,*cvalue ? cvalue : "ascii");CHKERRQ(ierr); 88 ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr); 89 ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr); 90 ierr = PetscObjectComposeFunction((PetscObject)*viewer,"PetscOptionsDestroyViewer","PetscViewerDestroy",(void (*)(void))PetscViewerDestroy);CHKERRQ(ierr); 91 } 92 } 93 ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr); 94 if (loc2 && *loc2) { 95 ierr = PetscStrtoupper(loc2);CHKERRQ(ierr); 96 ierr = PetscStrendswithwhich(loc2,PetscViewerFormats,&cnt);CHKERRQ(ierr); 97 if (!PetscViewerFormats[cnt]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2);CHKERRQ(ierr); 98 ierr = PetscViewerPushFormat(*viewer,(PetscViewerFormat)cnt);CHKERRQ(ierr); 99 ierr = PetscObjectComposeFunction((PetscObject)*viewer,"PetscOptionsPopViewer","PetscViewerPopFormat",(void (*)(void))PetscViewerPopFormat);CHKERRQ(ierr); 100 } 101 ierr = PetscFree(cvalue);CHKERRQ(ierr); 102 } 103 } 104 PetscFunctionReturn(0); 105 } 106 107 #undef __FUNCT__ 108 #define __FUNCT__ "PetscOptionsRestoreViewer" 109 /*@C 110 PetscOptionsRestoresViewer - Restores a viewer obtained with PetscOptionsGetViewer() 111 112 Collective on PetscViewer 113 114 Input Parameters: 115 . viewer - the viewer 116 117 Level: intermediate 118 119 Notes: Should only be called on the viewer if the call to PetscOptionsGetViewer() was successful and actually obtained a viewer 120 121 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 122 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 123 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 124 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 125 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 126 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 127 PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer() 128 @*/ 129 PetscErrorCode PetscOptionsRestoreViewer(PetscViewer viewer) 130 { 131 PetscErrorCode ierr,(*f)(PetscViewer*),(*g)(PetscViewer); 132 133 PetscFunctionBegin; 134 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsPopViewer",(void (**)(void))&g);CHKERRQ(ierr); 135 if (g) { 136 ierr = (*g)(viewer);CHKERRQ(ierr); 137 } 138 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsDestroyViewer",(void (**)(void))&f);CHKERRQ(ierr); 139 if (f) { 140 ierr = (*f)(&viewer);CHKERRQ(ierr); 141 } 142 PetscFunctionReturn(0); 143 } 144 145 #undef __FUNCT__ 146 #define __FUNCT__ "PetscViewerCreate" 147 /*@ 148 PetscViewerCreate - Creates a viewing context 149 150 Collective on MPI_Comm 151 152 Input Parameter: 153 . comm - MPI communicator 154 155 Output Parameter: 156 . inviewer - location to put the PetscViewer context 157 158 Level: advanced 159 160 Concepts: graphics^creating PetscViewer 161 Concepts: file input/output^creating PetscViewer 162 Concepts: sockets^creating PetscViewer 163 164 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 165 166 @*/ 167 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 168 { 169 PetscViewer viewer; 170 PetscErrorCode ierr; 171 172 PetscFunctionBegin; 173 *inviewer = 0; 174 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 175 ierr = PetscViewerInitializePackage(PETSC_NULL);CHKERRQ(ierr); 176 #endif 177 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr); 178 *inviewer = viewer; 179 viewer->data = 0; 180 PetscFunctionReturn(0); 181 } 182 183 #undef __FUNCT__ 184 #define __FUNCT__ "PetscViewerSetType" 185 /*@C 186 PetscViewerSetType - Builds PetscViewer for a particular implementation. 187 188 Collective on PetscViewer 189 190 Input Parameter: 191 + viewer - the PetscViewer context 192 - type - for example, "ASCII" 193 194 Options Database Command: 195 . -draw_type <type> - Sets the type; use -help for a list 196 of available methods (for instance, ascii) 197 198 Level: advanced 199 200 Notes: 201 See "include/petscviewer.h" for available methods (for instance, 202 PETSC_VIEWER_SOCKET) 203 204 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType 205 @*/ 206 PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type) 207 { 208 PetscErrorCode ierr,(*r)(PetscViewer); 209 PetscBool match; 210 211 PetscFunctionBegin; 212 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 213 PetscValidCharPointer(type,2); 214 CHKMEMQ; 215 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 216 if (match) PetscFunctionReturn(0); 217 218 /* cleanup any old type that may be there */ 219 if (viewer->data) { 220 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 221 viewer->ops->destroy = PETSC_NULL; 222 viewer->data = 0; 223 } 224 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 225 226 ierr = PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 227 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type); 228 229 ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr); 230 ierr = (*r)(viewer);CHKERRQ(ierr); 231 PetscFunctionReturn(0); 232 } 233 234 #undef __FUNCT__ 235 #define __FUNCT__ "PetscViewerRegisterDestroy" 236 /*@C 237 PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were 238 registered by PetscViewerRegisterDynamic(). 239 240 Not Collective 241 242 Level: developer 243 244 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll() 245 @*/ 246 PetscErrorCode PetscViewerRegisterDestroy(void) 247 { 248 PetscErrorCode ierr; 249 250 PetscFunctionBegin; 251 ierr = PetscFListDestroy(&PetscViewerList);CHKERRQ(ierr); 252 PetscFunctionReturn(0); 253 } 254 255 #undef __FUNCT__ 256 #define __FUNCT__ "PetscViewerRegister" 257 PetscErrorCode PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer)) 258 { 259 PetscErrorCode ierr; 260 char fullname[PETSC_MAX_PATH_LEN]; 261 262 PetscFunctionBegin; 263 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 264 ierr = PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 265 PetscFunctionReturn(0); 266 } 267 268 #undef __FUNCT__ 269 #define __FUNCT__ "PetscViewerSetFromOptions" 270 /*@C 271 PetscViewerSetFromOptions - Sets the graphics type from the options database. 272 Defaults to a PETSc X windows graphics. 273 274 Collective on PetscViewer 275 276 Input Parameter: 277 . PetscViewer - the graphics context 278 279 Level: intermediate 280 281 Notes: 282 Must be called after PetscViewerCreate() before the PetscViewer is used. 283 284 Concepts: PetscViewer^setting options 285 286 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 287 288 @*/ 289 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 290 { 291 PetscErrorCode ierr; 292 char vtype[256]; 293 PetscBool flg; 294 295 PetscFunctionBegin; 296 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 297 298 if (!PetscViewerList) { 299 ierr = PetscViewerRegisterAll(PETSC_NULL);CHKERRQ(ierr); 300 } 301 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 302 ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(((PetscObject)viewer)->type_name?((PetscObject)viewer)->type_name:PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 303 if (flg) { 304 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 305 } 306 /* type has not been set? */ 307 if (!((PetscObject)viewer)->type_name) { 308 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 309 } 310 if (viewer->ops->setfromoptions) { 311 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 312 } 313 314 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 315 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 316 ierr = PetscOptionsEnd();CHKERRQ(ierr); 317 318 PetscFunctionReturn(0); 319 } 320