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