1 2 #include <petsc-private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 4 PetscFunctionList PetscViewerList = 0; 5 6 #undef __FUNCT__ 7 #define __FUNCT__ "PetscOptionsGetViewer" 8 /*@C 9 PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user 10 11 Collective on MPI_Comm 12 13 Input Parameters: 14 + comm - the communicator to own the viewer 15 . pre - the string to prepend to the name or NULL 16 - name - the option one is seeking 17 18 Output Parameter: 19 + viewer - the viewer 20 . format - the PetscViewerFormat requested by the user 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 ascii_info, ascii_info_detail, or ascii_matlab, for example ascii::ascii_info prints just the info 27 $ about the object to standard out 28 $ binary[:[filename][:format]] defaults to binaryoutput 29 $ draw 30 $ socket[:port] defaults to the standard output port 31 32 Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will 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() 41 @*/ 42 PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool *set) 43 { 44 char *value; 45 PetscErrorCode ierr; 46 PetscBool flag; 47 48 PetscFunctionBegin; 49 PetscValidCharPointer(name,3); 50 51 if (format) *format = PETSC_VIEWER_DEFAULT; 52 if (set) *set = PETSC_FALSE; 53 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 54 if (flag) { 55 if (set) *set = PETSC_TRUE; 56 if (!value) { 57 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 58 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 59 } else { 60 char *cvalue,*loc,*loc2 = NULL; 61 PetscInt cnt; 62 const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERVTK,0}; 63 64 ierr = PetscStrallocpy(value,&cvalue);CHKERRQ(ierr); 65 ierr = PetscStrchr(cvalue,':',&loc);CHKERRQ(ierr); 66 if (loc) {*loc = 0; loc++;} 67 ierr = PetscStrendswithwhich(*cvalue ? cvalue : "ascii",viewers,&cnt);CHKERRQ(ierr); 68 if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",cvalue); 69 if (!loc) { 70 switch (cnt) { 71 case 0: 72 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 73 break; 74 case 1: 75 *viewer = PETSC_VIEWER_BINARY_(comm);CHKERRQ(ierr); 76 break; 77 case 2: 78 *viewer = PETSC_VIEWER_DRAW_(comm);CHKERRQ(ierr); 79 break; 80 #if defined(PETSC_USE_SOCKET_VIEWER) 81 case 3: 82 *viewer = PETSC_VIEWER_SOCKET_(comm);CHKERRQ(ierr); 83 break; 84 #endif 85 #if defined(PETSC_HAVE_MATLAB_ENGINE) 86 case 4: 87 *viewer = PETSC_VIEWER_MATLAB_(comm);CHKERRQ(ierr); 88 break; 89 #endif 90 default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",cvalue);CHKERRQ(ierr); 91 break; 92 } 93 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 94 } else { 95 ierr = PetscStrchr(loc,':',&loc2);CHKERRQ(ierr); 96 if (loc2) {*loc2 = 0; loc2++;} 97 if (loc2 && !*loc && (cnt == 0)) { /* ASCII format without file name */ 98 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 99 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 100 } else { 101 ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr); 102 ierr = PetscViewerSetType(*viewer,*cvalue ? cvalue : "ascii");CHKERRQ(ierr); 103 ierr = PetscViewerFileSetMode(*viewer,FILE_MODE_WRITE);CHKERRQ(ierr); 104 ierr = PetscViewerFileSetName(*viewer,loc);CHKERRQ(ierr); 105 } 106 } 107 ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr); 108 if (loc2 && *loc2) { 109 ierr = PetscStrtoupper(loc2);CHKERRQ(ierr); 110 ierr = PetscStrendswithwhich(loc2,PetscViewerFormats,&cnt);CHKERRQ(ierr); 111 if (!PetscViewerFormats[cnt]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2);CHKERRQ(ierr); 112 if (format) *format = (PetscViewerFormat)cnt; 113 } 114 ierr = PetscFree(cvalue);CHKERRQ(ierr); 115 } 116 } 117 PetscFunctionReturn(0); 118 } 119 120 #undef __FUNCT__ 121 #define __FUNCT__ "PetscViewerCreate" 122 /*@ 123 PetscViewerCreate - Creates a viewing context 124 125 Collective on MPI_Comm 126 127 Input Parameter: 128 . comm - MPI communicator 129 130 Output Parameter: 131 . inviewer - location to put the PetscViewer context 132 133 Level: advanced 134 135 Concepts: graphics^creating PetscViewer 136 Concepts: file input/output^creating PetscViewer 137 Concepts: sockets^creating PetscViewer 138 139 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 140 141 @*/ 142 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 143 { 144 PetscViewer viewer; 145 PetscErrorCode ierr; 146 147 PetscFunctionBegin; 148 *inviewer = 0; 149 #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 150 ierr = PetscViewerInitializePackage(NULL);CHKERRQ(ierr); 151 #endif 152 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr); 153 *inviewer = viewer; 154 viewer->data = 0; 155 PetscFunctionReturn(0); 156 } 157 158 #undef __FUNCT__ 159 #define __FUNCT__ "PetscViewerSetType" 160 /*@C 161 PetscViewerSetType - Builds PetscViewer for a particular implementation. 162 163 Collective on PetscViewer 164 165 Input Parameter: 166 + viewer - the PetscViewer context 167 - type - for example, "ASCII" 168 169 Options Database Command: 170 . -draw_type <type> - Sets the type; use -help for a list 171 of available methods (for instance, ascii) 172 173 Level: advanced 174 175 Notes: 176 See "include/petscviewer.h" for available methods (for instance, 177 PETSC_VIEWER_SOCKET) 178 179 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType 180 @*/ 181 PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type) 182 { 183 PetscErrorCode ierr,(*r)(PetscViewer); 184 PetscBool match; 185 186 PetscFunctionBegin; 187 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 188 PetscValidCharPointer(type,2); 189 CHKMEMQ; 190 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 191 if (match) PetscFunctionReturn(0); 192 193 /* cleanup any old type that may be there */ 194 if (viewer->data) { 195 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 196 197 viewer->ops->destroy = NULL; 198 viewer->data = 0; 199 } 200 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 201 202 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)viewer),PetscViewerList,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 = PetscFunctionListDestroy(&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 = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 240 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&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(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 PetscFunctionReturn(0); 294 } 295