1 2 #include <petsc-private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 4 PetscFunctionList 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 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 = 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(NULL);CHKERRQ(ierr); 152 #endif 153 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"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 198 viewer->ops->destroy = NULL; 199 viewer->data = 0; 200 } 201 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 202 203 ierr = PetscFunctionListFind(PetscObjectComm((PetscObject)viewer),PetscViewerList,type,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 204 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type); 205 206 ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr); 207 ierr = (*r)(viewer);CHKERRQ(ierr); 208 PetscFunctionReturn(0); 209 } 210 211 #undef __FUNCT__ 212 #define __FUNCT__ "PetscViewerRegisterDestroy" 213 /*@C 214 PetscViewerRegisterDestroy - Frees the list of PetscViewer methods that were 215 registered by PetscViewerRegisterDynamic(). 216 217 Not Collective 218 219 Level: developer 220 221 .seealso: PetscViewerRegisterDynamic(), PetscViewerRegisterAll() 222 @*/ 223 PetscErrorCode PetscViewerRegisterDestroy(void) 224 { 225 PetscErrorCode ierr; 226 227 PetscFunctionBegin; 228 ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr); 229 PetscFunctionReturn(0); 230 } 231 232 #undef __FUNCT__ 233 #define __FUNCT__ "PetscViewerRegister" 234 PetscErrorCode PetscViewerRegister(const char *sname,const char *path,const char *name,PetscErrorCode (*function)(PetscViewer)) 235 { 236 PetscErrorCode ierr; 237 char fullname[PETSC_MAX_PATH_LEN]; 238 239 PetscFunctionBegin; 240 ierr = PetscFunctionListConcat(path,name,fullname);CHKERRQ(ierr); 241 ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&PetscViewerList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 242 PetscFunctionReturn(0); 243 } 244 245 #undef __FUNCT__ 246 #define __FUNCT__ "PetscViewerSetFromOptions" 247 /*@C 248 PetscViewerSetFromOptions - Sets the graphics type from the options database. 249 Defaults to a PETSc X windows graphics. 250 251 Collective on PetscViewer 252 253 Input Parameter: 254 . PetscViewer - the graphics context 255 256 Level: intermediate 257 258 Notes: 259 Must be called after PetscViewerCreate() before the PetscViewer is used. 260 261 Concepts: PetscViewer^setting options 262 263 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 264 265 @*/ 266 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 267 { 268 PetscErrorCode ierr; 269 char vtype[256]; 270 PetscBool flg; 271 272 PetscFunctionBegin; 273 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 274 275 if (!PetscViewerList) { 276 ierr = PetscViewerRegisterAll(NULL);CHKERRQ(ierr); 277 } 278 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 279 ierr = PetscOptionsList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 280 if (flg) { 281 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 282 } 283 /* type has not been set? */ 284 if (!((PetscObject)viewer)->type_name) { 285 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 286 } 287 if (viewer->ops->setfromoptions) { 288 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 289 } 290 291 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 292 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 293 ierr = PetscOptionsEnd();CHKERRQ(ierr); 294 PetscFunctionReturn(0); 295 } 296