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) { /* 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 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 120 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 121 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 122 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 123 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 124 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 125 PetscOptionsList(), PetscOptionsEList(), PetscOptionsRestoreViewer() 126 @*/ 127 PetscErrorCode PetscOptionsRestoreViewer(PetscViewer viewer) 128 { 129 PetscErrorCode ierr,(*f)(PetscViewer*),(*g)(PetscViewer); 130 131 PetscFunctionBegin; 132 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsPopViewer",(void (**)(void))&g);CHKERRQ(ierr); 133 if (g) { 134 ierr = (*g)(viewer);CHKERRQ(ierr); 135 } 136 ierr = PetscObjectQueryFunction((PetscObject)viewer,"PetscOptionsDestroyViewer",(void (**)(void))&f);CHKERRQ(ierr); 137 if (f) { 138 ierr = (*f)(&viewer);CHKERRQ(ierr); 139 } 140 PetscFunctionReturn(0); 141 } 142 143 #undef __FUNCT__ 144 #define __FUNCT__ "PetscViewerCreate" 145 /*@ 146 PetscViewerCreate - Creates a viewing context 147 148 Collective on MPI_Comm 149 150 Input Parameter: 151 . comm - MPI communicator 152 153 Output Parameter: 154 . inviewer - location to put the PetscViewer context 155 156 Level: advanced 157 158 Concepts: graphics^creating PetscViewer 159 Concepts: file input/output^creating PetscViewer 160 Concepts: sockets^creating PetscViewer 161 162 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 163 164 @*/ 165 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 166 { 167 PetscViewer viewer; 168 PetscErrorCode ierr; 169 170 PetscFunctionBegin; 171 *inviewer = 0; 172 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 173 ierr = PetscViewerInitializePackage(PETSC_NULL);CHKERRQ(ierr); 174 #endif 175 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,-1,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr); 176 *inviewer = viewer; 177 viewer->data = 0; 178 PetscFunctionReturn(0); 179 } 180 181 #undef __FUNCT__ 182 #define __FUNCT__ "PetscViewerSetType" 183 /*@C 184 PetscViewerSetType - Builds PetscViewer for a particular implementation. 185 186 Collective on PetscViewer 187 188 Input Parameter: 189 + viewer - the PetscViewer context 190 - type - for example, "ASCII" 191 192 Options Database Command: 193 . -draw_type <type> - Sets the type; use -help for a list 194 of available methods (for instance, ascii) 195 196 Level: advanced 197 198 Notes: 199 See "include/petscviewer.h" for available methods (for instance, 200 PETSC_VIEWER_SOCKET) 201 202 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType 203 @*/ 204 PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type) 205 { 206 PetscErrorCode ierr,(*r)(PetscViewer); 207 PetscBool match; 208 209 PetscFunctionBegin; 210 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 211 PetscValidCharPointer(type,2); 212 CHKMEMQ; 213 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 214 if (match) PetscFunctionReturn(0); 215 216 /* cleanup any old type that may be there */ 217 if (viewer->data) { 218 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 219 viewer->ops->destroy = PETSC_NULL; 220 viewer->data = 0; 221 } 222 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 223 224 ierr = PetscFListFind(PetscViewerList,((PetscObject)viewer)->comm,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 225 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type); 226 227 ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr); 228 ierr = (*r)(viewer);CHKERRQ(ierr); 229 PetscFunctionReturn(0); 230 } 231 232 #undef __FUNCT__ 233 #define __FUNCT__ "PetscViewerRegisterDestroy" 234 /*@C 235 PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were 236 registered by PetscViewerRegisterDynamic(). 237 238 Not Collective 239 240 Level: developer 241 242 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll() 243 @*/ 244 PetscErrorCode PetscViewerRegisterDestroy(void) 245 { 246 PetscErrorCode ierr; 247 248 PetscFunctionBegin; 249 ierr = PetscFListDestroy(&PetscViewerList);CHKERRQ(ierr); 250 PetscFunctionReturn(0); 251 } 252 253 #undef __FUNCT__ 254 #define __FUNCT__ "PetscViewerRegister" 255 PetscErrorCode PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer)) 256 { 257 PetscErrorCode ierr; 258 char fullname[PETSC_MAX_PATH_LEN]; 259 260 PetscFunctionBegin; 261 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 262 ierr = PetscFListAdd(&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 263 PetscFunctionReturn(0); 264 } 265 266 #undef __FUNCT__ 267 #define __FUNCT__ "PetscViewerSetFromOptions" 268 /*@C 269 PetscViewerSetFromOptions - Sets the graphics type from the options database. 270 Defaults to a PETSc X windows graphics. 271 272 Collective on PetscViewer 273 274 Input Parameter: 275 . PetscViewer - the graphics context 276 277 Level: intermediate 278 279 Notes: 280 Must be called after PetscViewerCreate() before the PetscViewer is used. 281 282 Concepts: PetscViewer^setting options 283 284 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 285 286 @*/ 287 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 288 { 289 PetscErrorCode ierr; 290 char vtype[256]; 291 PetscBool flg; 292 293 PetscFunctionBegin; 294 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 295 296 if (!PetscViewerList) { 297 ierr = PetscViewerRegisterAll(PETSC_NULL);CHKERRQ(ierr); 298 } 299 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 300 ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char *)(((PetscObject)viewer)->type_name?((PetscObject)viewer)->type_name:PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 301 if (flg) { 302 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 303 } 304 /* type has not been set? */ 305 if (!((PetscObject)viewer)->type_name) { 306 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 307 } 308 if (viewer->ops->setfromoptions) { 309 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 310 } 311 312 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 313 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 314 ierr = PetscOptionsEnd();CHKERRQ(ierr); 315 316 PetscFunctionReturn(0); 317 } 318