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 $ ams[: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,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 default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype); 109 } 110 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 111 } else { 112 if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */ 113 ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr); 114 ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr); 115 } else { 116 PetscFileMode fmode; 117 ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr); 118 ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr); 119 fmode = FILE_MODE_WRITE; 120 if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */ 121 ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr); 122 if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode); 123 } 124 ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr); 125 ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr); 126 ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr); 127 } 128 } 129 if (loc2_fmt && *loc2_fmt) { 130 ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr); 131 if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr); 132 } 133 ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr); 134 ierr = PetscFree(loc0_vtype);CHKERRQ(ierr); 135 } 136 } 137 PetscFunctionReturn(0); 138 } 139 140 #undef __FUNCT__ 141 #define __FUNCT__ "PetscViewerCreate" 142 /*@ 143 PetscViewerCreate - Creates a viewing context 144 145 Collective on MPI_Comm 146 147 Input Parameter: 148 . comm - MPI communicator 149 150 Output Parameter: 151 . inviewer - location to put the PetscViewer context 152 153 Level: advanced 154 155 Concepts: graphics^creating PetscViewer 156 Concepts: file input/output^creating PetscViewer 157 Concepts: sockets^creating PetscViewer 158 159 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType 160 161 @*/ 162 PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer) 163 { 164 PetscViewer viewer; 165 PetscErrorCode ierr; 166 167 PetscFunctionBegin; 168 *inviewer = 0; 169 ierr = PetscViewerInitializePackage();CHKERRQ(ierr); 170 ierr = PetscHeaderCreate(viewer,_p_PetscViewer,struct _PetscViewerOps,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,0);CHKERRQ(ierr); 171 *inviewer = viewer; 172 viewer->data = 0; 173 PetscFunctionReturn(0); 174 } 175 176 #undef __FUNCT__ 177 #define __FUNCT__ "PetscViewerSetType" 178 /*@C 179 PetscViewerSetType - Builds PetscViewer for a particular implementation. 180 181 Collective on PetscViewer 182 183 Input Parameter: 184 + viewer - the PetscViewer context 185 - type - for example, PETSCVIEWERASCII 186 187 Options Database Command: 188 . -draw_type <type> - Sets the type; use -help for a list 189 of available methods (for instance, ascii) 190 191 Level: advanced 192 193 Notes: 194 See "include/petscviewer.h" for available methods (for instance, 195 PETSCVIEWERSOCKET) 196 197 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerSetFormat() 198 @*/ 199 PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type) 200 { 201 PetscErrorCode ierr,(*r)(PetscViewer); 202 PetscBool match; 203 204 PetscFunctionBegin; 205 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 206 PetscValidCharPointer(type,2); 207 ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr); 208 if (match) PetscFunctionReturn(0); 209 210 /* cleanup any old type that may be there */ 211 if (viewer->data) { 212 ierr = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr); 213 214 viewer->ops->destroy = NULL; 215 viewer->data = 0; 216 } 217 ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr); 218 219 ierr = PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr); 220 if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type); 221 222 ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr); 223 ierr = (*r)(viewer);CHKERRQ(ierr); 224 PetscFunctionReturn(0); 225 } 226 227 #undef __FUNCT__ 228 #define __FUNCT__ "PetscViewerRegister" 229 /*@C 230 PetscViewerRegister - Adds a viewer 231 232 Not Collective 233 234 Input Parameters: 235 + name_solver - name of a new user-defined viewer 236 - routine_create - routine to create method context 237 238 Level: developer 239 Notes: 240 PetscViewerRegister() may be called multiple times to add several user-defined viewers. 241 242 Sample usage: 243 .vb 244 PetscViewerRegister("my_viewer_type",MyViewerCreate); 245 .ve 246 247 Then, your solver can be chosen with the procedural interface via 248 $ PetscViewerSetType(viewer,"my_viewer_type") 249 or at runtime via the option 250 $ -viewer_type my_viewer_type 251 252 Concepts: registering^Viewers 253 254 .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy() 255 @*/ 256 PetscErrorCode PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer)) 257 { 258 PetscErrorCode ierr; 259 260 PetscFunctionBegin; 261 ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr); 262 PetscFunctionReturn(0); 263 } 264 265 #undef __FUNCT__ 266 #define __FUNCT__ "PetscViewerSetFromOptions" 267 /*@C 268 PetscViewerSetFromOptions - Sets the graphics type from the options database. 269 Defaults to a PETSc X windows graphics. 270 271 Collective on PetscViewer 272 273 Input Parameter: 274 . PetscViewer - the graphics context 275 276 Level: intermediate 277 278 Notes: 279 Must be called after PetscViewerCreate() before the PetscViewer is used. 280 281 Concepts: PetscViewer^setting options 282 283 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 284 285 @*/ 286 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer) 287 { 288 PetscErrorCode ierr; 289 char vtype[256]; 290 PetscBool flg; 291 292 PetscFunctionBegin; 293 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 294 295 if (!PetscViewerList) { 296 ierr = PetscViewerRegisterAll();CHKERRQ(ierr); 297 } 298 ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr); 299 ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr); 300 if (flg) { 301 ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr); 302 } 303 /* type has not been set? */ 304 if (!((PetscObject)viewer)->type_name) { 305 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 306 } 307 if (viewer->ops->setfromoptions) { 308 ierr = (*viewer->ops->setfromoptions)(viewer);CHKERRQ(ierr); 309 } 310 311 /* process any options handlers added with PetscObjectAddOptionsHandler() */ 312 ierr = PetscObjectProcessOptionsHandlers((PetscObject)viewer);CHKERRQ(ierr); 313 ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr); 314 ierr = PetscOptionsEnd();CHKERRQ(ierr); 315 PetscFunctionReturn(0); 316 } 317