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,PETSCVIEWERMATLAB,PETSCVIEWERVTK,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 > sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",cvalue); 67 if (!loc) { 68 switch (cnt) { 69 case 0: 70 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 71 break; 72 case 1: 73 *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr); 74 break; 75 case 2: 76 *viewer = PETSC_VIEWER_DRAW_(comm);CHKERRQ(ierr); 77 break; 78 #if defined(PETSC_USE_SOCKET_VIEWER) 79 case 3: 80 *viewer = PETSC_VIEWER_SOCKET_(comm);CHKERRQ(ierr); 81 break; 82 #endif 83 #if defined(PETSC_HAVE_MATLAB_ENGINE) 84 case 4: 85 *viewer = PETSC_VIEWER_MATLAB_(comm);CHKERRQ(ierr); 86 break; 87 #endif 88 default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",cvalue);CHKERRQ(ierr); 89 } 90 } else { 91 ierr = PetscStrchr(loc,':',&loc2);CHKERRQ(ierr); 92 if (loc2) {*loc2 = 0; loc2++;} 93 if (loc2 && !*loc && (cnt == 0)) { /* ASCII format without file name */ 94 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 95 } else { 96 ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr); 97 ierr = PetscViewerSetType(*viewer,*cvalue ? cvalue : "ascii");CHKERRQ(ierr); 98 ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr); 99 ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr); 100 ierr = PetscObjectComposeFunction((PetscObject)*viewer,"PetscOptionsDestroyViewer","PetscViewerDestroy",(void (*)(void))PetscViewerDestroy);CHKERRQ(ierr); 101 } 102 } 103 ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr); 104 if (loc2 && *loc2) { 105 ierr = PetscStrtoupper(loc2);CHKERRQ(ierr); 106 ierr = PetscStrendswithwhich(loc2,PetscViewerFormats,&cnt);CHKERRQ(ierr); 107 if (!PetscViewerFormats[cnt]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2);CHKERRQ(ierr); 108 ierr = PetscViewerPushFormat(*viewer,(PetscViewerFormat)cnt);CHKERRQ(ierr); 109 ierr = PetscObjectComposeFunction((PetscObject)*viewer,"PetscOptionsPopViewer","PetscViewerPopFormat",(void (*)(void))PetscViewerPopFormat);CHKERRQ(ierr); 110 } 111 ierr = PetscFree(cvalue);CHKERRQ(ierr); 112 } 113 } 114 PetscFunctionReturn(0); 115 } 116 117 #undef __FUNCT__ 118 #define __FUNCT__ "PetscOptionsRestoreViewer" 119 /*@C 120 PetscOptionsRestoresViewer - Restores a viewer obtained with PetscOptionsGetViewer() 121 122 Collective on PetscViewer 123 124 Input Parameters: 125 . viewer - the viewer 126 127 Level: intermediate 128 129 Notes: Should only be called on the viewer if the call to PetscOptionsGetViewer() was successful and actually obtained a viewer 130 131 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 132 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 133 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 134 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 135 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 136 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 137 PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer() 138 @*/ 139 PetscErrorCode PetscOptionsRestoreViewer(PetscViewer viewer) 140 { 141 PetscErrorCode ierr,(*f)(PetscViewer*),(*g)(PetscViewer); 142 143 PetscFunctionBegin; 144 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsPopViewer",(void (**)(void))&g);CHKERRQ(ierr); 145 if (g) { 146 ierr = (*g)(viewer);CHKERRQ(ierr); 147 } 148 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsDestroyViewer",(void (**)(void))&f);CHKERRQ(ierr); 149 if (f) { 150 ierr = (*f)(&viewer);CHKERRQ(ierr); 151 } 152 PetscFunctionReturn(0); 153 } 154 155 #undef __FUNCT__ 156 #define __FUNCT__ "PetscViewerCreate" 157 /*@ 158 PetscViewerCreate - Creates a viewing context 159 160 Collective on MPI_Comm 161 162 Input Parameter: 163 . comm - MPI communicator 164 165 Output Parameter: 166 . inviewer - location to put the PetscViewer context 167 168 Level: advanced 169 170 Concepts: graphics^creating PetscViewer 171 Concepts: file input/output^creating PetscViewer 172 Concepts: sockets^creating PetscViewer 173 174 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 175 176 @*/ 177 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 178 { 179 PetscViewer viewer; 180 PetscErrorCode ierr; 181 182 PetscFunctionBegin; 183 *inviewer = 0; 184 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 185 ierr = PetscViewerInitializePackage(PETSC_NULL);CHKERRQ(ierr); 186 #endif 187 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr); 188 *inviewer = viewer; 189 viewer->data = 0; 190 PetscFunctionReturn(0); 191 } 192 193 #undef __FUNCT__ 194 #define __FUNCT__ "PetscViewerSetType" 195 /*@C 196 PetscViewerSetType - Builds PetscViewer for a particular implementation. 197 198 Collective on PetscViewer 199 200 Input Parameter: 201 + viewer - the PetscViewer context 202 - type - for example, "ASCII" 203 204 Options Database Command: 205 . -draw_type <type> - Sets the type; use -help for a list 206 of available methods (for instance, ascii) 207 208 Level: advanced 209 210 Notes: 211 See "include/petscviewer.h" for available methods (for instance, 212 PETSC_VIEWER_SOCKET) 213 214 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType 215 @*/ 216 PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type) 217 { 218 PetscErrorCode ierr,(*r)(PetscViewer); 219 PetscBool match; 220 221 PetscFunctionBegin; 222 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 223 PetscValidCharPointer(type,2); 224 CHKMEMQ; 225 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 226 if (match) PetscFunctionReturn(0); 227 228 /* cleanup any old type that may be there */ 229 if (viewer->data) { 230 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 231 viewer->ops->destroy = PETSC_NULL; 232 viewer->data = 0; 233 } 234 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 235 236 ierr = PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 237 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type); 238 239 ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr); 240 ierr = (*r)(viewer);CHKERRQ(ierr); 241 PetscFunctionReturn(0); 242 } 243 244 #undef __FUNCT__ 245 #define __FUNCT__ "PetscViewerRegisterDestroy" 246 /*@C 247 PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were 248 registered by PetscViewerRegisterDynamic(). 249 250 Not Collective 251 252 Level: developer 253 254 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll() 255 @*/ 256 PetscErrorCode PetscViewerRegisterDestroy(void) 257 { 258 PetscErrorCode ierr; 259 260 PetscFunctionBegin; 261 ierr = PetscFListDestroy(&PetscViewerList);CHKERRQ(ierr); 262 PetscFunctionReturn(0); 263 } 264 265 #undef __FUNCT__ 266 #define __FUNCT__ "PetscViewerRegister" 267 PetscErrorCode PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer)) 268 { 269 PetscErrorCode ierr; 270 char fullname[PETSC_MAX_PATH_LEN]; 271 272 PetscFunctionBegin; 273 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 274 ierr = PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 275 PetscFunctionReturn(0); 276 } 277 278 #undef __FUNCT__ 279 #define __FUNCT__ "PetscViewerSetFromOptions" 280 /*@C 281 PetscViewerSetFromOptions - Sets the graphics type from the options database. 282 Defaults to a PETSc X windows graphics. 283 284 Collective on PetscViewer 285 286 Input Parameter: 287 . PetscViewer - the graphics context 288 289 Level: intermediate 290 291 Notes: 292 Must be called after PetscViewerCreate() before the PetscViewer is used. 293 294 Concepts: PetscViewer^setting options 295 296 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 297 298 @*/ 299 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 300 { 301 PetscErrorCode ierr; 302 char vtype[256]; 303 PetscBool flg; 304 305 PetscFunctionBegin; 306 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 307 308 if (!PetscViewerList) { 309 ierr = PetscViewerRegisterAll(PETSC_NULL);CHKERRQ(ierr); 310 } 311 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 312 ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(((PetscObject)viewer)->type_name?((PetscObject)viewer)->type_name:PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 313 if (flg) { 314 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 315 } 316 /* type has not been set? */ 317 if (!((PetscObject)viewer)->type_name) { 318 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 319 } 320 if (viewer->ops->setfromoptions) { 321 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 322 } 323 324 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 325 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 326 ierr = PetscOptionsEnd();CHKERRQ(ierr); 327 328 PetscFunctionReturn(0); 329 } 330