1 2 #include <petsc-private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 #if defined(PETSC_HAVE_SAWS) 4 #include <petscviewersaws.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][:append]]] defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab, 30 for example ascii::ascii_info prints just the information about the object not all details 31 unless :append is given filename opens in write mode, overwriting what was already there 32 $ binary[:[filename][:[format][:append]]] defaults to the file binaryoutput 33 $ draw[:drawtype} for example, draw:tikz or draw:x 34 $ socket[:port] defaults to the standard output port 35 $ saws[:communicatorname] publishes object to the Scientific Application Webserver (SAWs) 36 37 Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur 38 39 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 40 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 41 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 42 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 43 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 44 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 45 PetscOptionsFList(), PetscOptionsEList() 46 @*/ 47 PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool *set) 48 { 49 char *value; 50 PetscErrorCode ierr; 51 PetscBool flag; 52 53 PetscFunctionBegin; 54 PetscValidCharPointer(name,3); 55 56 if (format) *format = PETSC_VIEWER_DEFAULT; 57 if (set) *set = PETSC_FALSE; 58 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 59 if (flag) { 60 if (set) *set = PETSC_TRUE; 61 if (!value) { 62 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 63 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 64 } else { 65 char *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL; 66 PetscInt cnt; 67 const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0}; 68 69 ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr); 70 ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr); 71 if (loc1_fname) { 72 *loc1_fname++ = 0; 73 ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr); 74 } 75 if (loc2_fmt) { 76 *loc2_fmt++ = 0; 77 ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr); 78 } 79 if (loc3_fmode) *loc3_fmode++ = 0; 80 ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr); 81 if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype); 82 if (!loc1_fname) { 83 switch (cnt) { 84 case 0: 85 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 86 break; 87 case 1: 88 if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB); 89 break; 90 case 2: 91 if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB); 92 break; 93 #if defined(PETSC_USE_SOCKET_VIEWER) 94 case 3: 95 if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB); 96 break; 97 #endif 98 #if defined(PETSC_HAVE_MATLAB_ENGINE) 99 case 4: 100 if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB); 101 break; 102 #endif 103 #if defined(PETSC_HAVE_SAWS) 104 case 5: 105 if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB); 106 break; 107 #endif 108 #if defined(PETSC_HAVE_HDF5) 109 case 7: 110 if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB); 111 break; 112 #endif 113 default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype); 114 } 115 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 116 } else { 117 if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */ 118 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 119 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 120 } else { 121 PetscFileMode fmode; 122 ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr); 123 ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr); 124 fmode = FILE_MODE_WRITE; 125 if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */ 126 ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr); 127 if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode); 128 } 129 ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr); 130 ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr); 131 ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr); 132 } 133 } 134 if (loc2_fmt && *loc2_fmt) { 135 ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr); 136 if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr); 137 } 138 ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr); 139 ierr = PetscFree(loc0_vtype);CHKERRQ(ierr); 140 } 141 } 142 PetscFunctionReturn(0); 143 } 144 145 #undef __FUNCT__ 146 #define __FUNCT__ "PetscViewerCreate" 147 /*@ 148 PetscViewerCreate - Creates a viewing context 149 150 Collective on MPI_Comm 151 152 Input Parameter: 153 . comm - MPI communicator 154 155 Output Parameter: 156 . inviewer - location to put the PetscViewer context 157 158 Level: advanced 159 160 Concepts: graphics^creating PetscViewer 161 Concepts: file input/output^creating PetscViewer 162 Concepts: sockets^creating PetscViewer 163 164 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 165 166 @*/ 167 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 168 { 169 PetscViewer viewer; 170 PetscErrorCode ierr; 171 172 PetscFunctionBegin; 173 *inviewer = 0; 174 ierr = PetscViewerInitializePackage();CHKERRQ(ierr); 175 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"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, PETSCVIEWERASCII 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 PETSCVIEWERSOCKET) 201 202 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat() 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 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 213 if (match) PetscFunctionReturn(0); 214 215 /* cleanup any old type that may be there */ 216 if (viewer->data) { 217 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 218 219 viewer->ops->destroy = NULL; 220 viewer->data = 0; 221 } 222 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 223 224 ierr = PetscFunctionListFind(PetscViewerList,type,&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__ "PetscViewerRegister" 234 /*@C 235 PetscViewerRegister - Adds a viewer 236 237 Not Collective 238 239 Input Parameters: 240 + name_solver - name of a new user-defined viewer 241 - routine_create - routine to create method context 242 243 Level: developer 244 Notes: 245 PetscViewerRegister() may be called multiple times to add several user-defined viewers. 246 247 Sample usage: 248 .vb 249 PetscViewerRegister("my_viewer_type",MyViewerCreate); 250 .ve 251 252 Then, your solver can be chosen with the procedural interface via 253 $ PetscViewerSetType(viewer,"my_viewer_type") 254 or at runtime via the option 255 $ -viewer_type my_viewer_type 256 257 Concepts: registering^Viewers 258 259 .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy() 260 @*/ 261 PetscErrorCode PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer)) 262 { 263 PetscErrorCode ierr; 264 265 PetscFunctionBegin; 266 ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr); 267 PetscFunctionReturn(0); 268 } 269 270 #undef __FUNCT__ 271 #define __FUNCT__ "PetscViewerSetFromOptions" 272 /*@C 273 PetscViewerSetFromOptions - Sets the graphics type from the options database. 274 Defaults to a PETSc X windows graphics. 275 276 Collective on PetscViewer 277 278 Input Parameter: 279 . PetscViewer - the graphics context 280 281 Level: intermediate 282 283 Notes: 284 Must be called after PetscViewerCreate() before the PetscViewer is used. 285 286 Concepts: PetscViewer^setting options 287 288 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 289 290 @*/ 291 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 292 { 293 PetscErrorCode ierr; 294 char vtype[256]; 295 PetscBool flg; 296 297 PetscFunctionBegin; 298 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 299 300 if (!PetscViewerList) { 301 ierr = PetscViewerRegisterAll();CHKERRQ(ierr); 302 } 303 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 304 ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 305 if (flg) { 306 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 307 } 308 /* type has not been set? */ 309 if (!((PetscObject)viewer)->type_name) { 310 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 311 } 312 if (viewer->ops->setfromoptions) { 313 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 314 } 315 316 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 317 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 318 ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr); 319 ierr = PetscOptionsEnd();CHKERRQ(ierr); 320 PetscFunctionReturn(0); 321 } 322