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