1 2 #include <petsc-private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 4 PetscClassId PETSC_VIEWER_CLASSID; 5 6 static PetscBool PetscViewerPackageInitialized = PETSC_FALSE; 7 #undef __FUNCT__ 8 #define __FUNCT__ "PetscViewerFinalizePackage" 9 /*@C 10 PetscViewerFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is 11 called from PetscFinalize(). 12 13 Level: developer 14 15 .keywords: Petsc, destroy, package, mathematica 16 .seealso: PetscFinalize() 17 @*/ 18 PetscErrorCode PetscViewerFinalizePackage(void) 19 { 20 PetscErrorCode ierr; 21 22 PetscFunctionBegin; 23 ierr = PetscFunctionListDestroy(&PetscViewerList);CHKERRQ(ierr); 24 PetscViewerPackageInitialized = PETSC_FALSE; 25 PetscViewerRegisterAllCalled = PETSC_FALSE; 26 PetscFunctionReturn(0); 27 } 28 29 #undef __FUNCT__ 30 #define __FUNCT__ "PetscViewerInitializePackage" 31 /*@C 32 PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package. 33 34 Level: developer 35 36 .keywords: Petsc, initialize, package 37 .seealso: PetscInitialize() 38 @*/ 39 PetscErrorCode PetscViewerInitializePackage(void) 40 { 41 char logList[256]; 42 char *className; 43 PetscBool opt; 44 PetscErrorCode ierr; 45 46 PetscFunctionBegin; 47 if (PetscViewerPackageInitialized) PetscFunctionReturn(0); 48 PetscViewerPackageInitialized = PETSC_TRUE; 49 /* Register Classes */ 50 ierr = PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);CHKERRQ(ierr); 51 52 /* Register Constructors */ 53 ierr = PetscViewerRegisterAll();CHKERRQ(ierr); 54 55 /* Process info exclusions */ 56 ierr = PetscOptionsGetString(NULL, "-info_exclude", logList, 256, &opt);CHKERRQ(ierr); 57 if (opt) { 58 ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr); 59 if (className) { 60 ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr); 61 } 62 } 63 /* Process summary exclusions */ 64 ierr = PetscOptionsGetString(NULL, "-log_summary_exclude", logList, 256, &opt);CHKERRQ(ierr); 65 if (opt) { 66 ierr = PetscStrstr(logList, "viewer", &className);CHKERRQ(ierr); 67 if (className) { 68 ierr = PetscLogEventDeactivateClass(0);CHKERRQ(ierr); 69 } 70 } 71 #if defined(PETSC_HAVE_MATHEMATICA) 72 ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr); 73 #endif 74 ierr = PetscRegisterFinalize(PetscViewerFinalizePackage);CHKERRQ(ierr); 75 PetscFunctionReturn(0); 76 } 77 78 #undef __FUNCT__ 79 #define __FUNCT__ "PetscViewerDestroy" 80 /*@ 81 PetscViewerDestroy - Destroys a PetscViewer. 82 83 Collective on PetscViewer 84 85 Input Parameters: 86 . viewer - the PetscViewer to be destroyed. 87 88 Level: beginner 89 90 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen() 91 92 @*/ 93 PetscErrorCode PetscViewerDestroy(PetscViewer *viewer) 94 { 95 PetscErrorCode ierr; 96 97 PetscFunctionBegin; 98 if (!*viewer) PetscFunctionReturn(0); 99 PetscValidHeaderSpecific(*viewer,PETSC_VIEWER_CLASSID,1); 100 101 ierr = PetscViewerFlush(*viewer);CHKERRQ(ierr); 102 if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; PetscFunctionReturn(0);} 103 104 ierr = PetscObjectSAWsViewOff((PetscObject)*viewer);CHKERRQ(ierr); 105 if ((*viewer)->ops->destroy) { 106 ierr = (*(*viewer)->ops->destroy)(*viewer);CHKERRQ(ierr); 107 } 108 ierr = PetscHeaderDestroy(viewer);CHKERRQ(ierr); 109 PetscFunctionReturn(0); 110 } 111 112 #undef __FUNCT__ 113 #define __FUNCT__ "PetscViewerGetType" 114 /*@C 115 PetscViewerGetType - Returns the type of a PetscViewer. 116 117 Not Collective 118 119 Input Parameter: 120 . viewer - the PetscViewer 121 122 Output Parameter: 123 . type - PetscViewer type (see below) 124 125 Available Types Include: 126 . PETSCVIEWERSOCKET - Socket PetscViewer 127 . PETSCVIEWERASCII - ASCII PetscViewer 128 . PETSCVIEWERBINARY - binary file PetscViewer 129 . PETSCVIEWERSTRING - string PetscViewer 130 . PETSCVIEWERDRAW - drawing PetscViewer 131 132 Level: intermediate 133 134 Note: 135 See include/petscviewer.h for a complete list of PetscViewers. 136 137 PetscViewerType is actually a string 138 139 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType 140 141 @*/ 142 PetscErrorCode PetscViewerGetType(PetscViewer viewer,PetscViewerType *type) 143 { 144 PetscFunctionBegin; 145 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 146 PetscValidPointer(type,2); 147 *type = ((PetscObject)viewer)->type_name; 148 PetscFunctionReturn(0); 149 } 150 151 #undef __FUNCT__ 152 #define __FUNCT__ "PetscViewerSetOptionsPrefix" 153 /*@C 154 PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all 155 PetscViewer options in the database. 156 157 Logically Collective on PetscViewer 158 159 Input Parameter: 160 + viewer - the PetscViewer context 161 - prefix - the prefix to prepend to all option names 162 163 Notes: 164 A hyphen (-) must NOT be given at the beginning of the prefix name. 165 The first character of all runtime options is AUTOMATICALLY the hyphen. 166 167 Level: advanced 168 169 .keywords: PetscViewer, set, options, prefix, database 170 171 .seealso: PetscViewerSetFromOptions() 172 @*/ 173 PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[]) 174 { 175 PetscErrorCode ierr; 176 177 PetscFunctionBegin; 178 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 179 ierr = PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 180 PetscFunctionReturn(0); 181 } 182 183 #undef __FUNCT__ 184 #define __FUNCT__ "PetscViewerAppendOptionsPrefix" 185 /*@C 186 PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all 187 PetscViewer options in the database. 188 189 Logically Collective on PetscViewer 190 191 Input Parameters: 192 + viewer - the PetscViewer context 193 - prefix - the prefix to prepend to all option names 194 195 Notes: 196 A hyphen (-) must NOT be given at the beginning of the prefix name. 197 The first character of all runtime options is AUTOMATICALLY the hyphen. 198 199 Level: advanced 200 201 .keywords: PetscViewer, append, options, prefix, database 202 203 .seealso: PetscViewerGetOptionsPrefix() 204 @*/ 205 PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[]) 206 { 207 PetscErrorCode ierr; 208 209 PetscFunctionBegin; 210 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 211 ierr = PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 212 PetscFunctionReturn(0); 213 } 214 215 #undef __FUNCT__ 216 #define __FUNCT__ "PetscViewerGetOptionsPrefix" 217 /*@C 218 PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all 219 PetscViewer options in the database. 220 221 Not Collective 222 223 Input Parameter: 224 . viewer - the PetscViewer context 225 226 Output Parameter: 227 . prefix - pointer to the prefix string used 228 229 Notes: On the fortran side, the user should pass in a string 'prefix' of 230 sufficient length to hold the prefix. 231 232 Level: advanced 233 234 .keywords: PetscViewer, get, options, prefix, database 235 236 .seealso: PetscViewerAppendOptionsPrefix() 237 @*/ 238 PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[]) 239 { 240 PetscErrorCode ierr; 241 242 PetscFunctionBegin; 243 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 244 ierr = PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);CHKERRQ(ierr); 245 PetscFunctionReturn(0); 246 } 247 248 #undef __FUNCT__ 249 #define __FUNCT__ "PetscViewerSetUp" 250 /*@ 251 PetscViewerSetUp - Sets up the internal viewer data structures for the later use. 252 253 Collective on PetscViewer 254 255 Input Parameters: 256 . viewer - the PetscViewer context 257 258 Notes: 259 For basic use of the PetscViewer classes the user need not explicitly call 260 PetscViewerSetUp(), since these actions will happen automatically. 261 262 Level: advanced 263 264 .keywords: PetscViewer, setup 265 266 .seealso: PetscViewerCreate(), PetscViewerDestroy() 267 @*/ 268 PetscErrorCode PetscViewerSetUp(PetscViewer viewer) 269 { 270 PetscFunctionBegin; 271 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 272 PetscFunctionReturn(0); 273 } 274 275 #undef __FUNCT__ 276 #define __FUNCT__ "PetscViewerView" 277 /*@C 278 PetscViewerView - Visualizes a viewer object. 279 280 Collective on PetscViewer 281 282 Input Parameters: 283 + v - the viewer 284 - viewer - visualization context 285 286 Notes: 287 The available visualization contexts include 288 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 289 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 290 output where only the first processor opens 291 the file. All other processors send their 292 data to the first processor to print. 293 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 294 295 Level: beginner 296 297 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 298 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad() 299 @*/ 300 PetscErrorCode PetscViewerView(PetscViewer v,PetscViewer viewer) 301 { 302 PetscErrorCode ierr; 303 PetscBool iascii; 304 PetscViewerFormat format; 305 #if defined(PETSC_HAVE_SAWS) 306 PetscBool issaws; 307 #endif 308 309 PetscFunctionBegin; 310 PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 311 PetscValidType(v,1); 312 if (!viewer) { 313 ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);CHKERRQ(ierr); 314 } 315 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 316 PetscCheckSameComm(v,1,viewer,2); 317 318 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 319 #if defined(PETSC_HAVE_SAWS) 320 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);CHKERRQ(ierr); 321 #endif 322 if (iascii) { 323 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 324 ierr = PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);CHKERRQ(ierr); 325 if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 326 if (v->format) { 327 ierr = PetscViewerASCIIPrintf(viewer," Viewer format = %s\n",PetscViewerFormats[v->format]);CHKERRQ(ierr); 328 } 329 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 330 if (v->ops->view) { 331 ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 332 } 333 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 334 } 335 #if defined(PETSC_HAVE_SAWS) 336 } else if (issaws) { 337 if (!((PetscObject)v)->amsmem) { 338 ierr = PetscObjectViewSAWs((PetscObject)v,viewer);CHKERRQ(ierr); 339 if (v->ops->view) { 340 ierr = (*v->ops->view)(v,viewer);CHKERRQ(ierr); 341 } 342 } 343 #endif 344 } 345 PetscFunctionReturn(0); 346 } 347 348 #undef __FUNCT__ 349 #define __FUNCT__ "PetscViewerRead" 350 /*@C 351 PetscViewerRead - Reads data from a PetscViewer 352 353 Collective on MPI_Comm 354 355 Input Parameters: 356 + viewer - The viewer 357 . data - Location to write the data 358 . count - Number of items of data to read 359 - datatype - Type of data to read 360 361 Level: beginner 362 363 Concepts: binary files, ascii files 364 365 .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 366 VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 367 PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer 368 @*/ 369 PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt count, PetscDataType dtype) 370 { 371 PetscErrorCode ierr; 372 373 PetscFunctionBegin; 374 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 375 if (dtype == PETSC_STRING) { 376 PetscInt c, i = 0; 377 char *s = (char *)data; 378 for (c = 0; c < count; c++) { 379 /* Skip leading whitespaces */ 380 do {ierr = (*viewer->ops->read)(viewer, &(s[i]), 1, PETSC_CHAR);CHKERRQ(ierr);} 381 while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0'); 382 i++; 383 /* Read strings one char at a time */ 384 do {ierr = (*viewer->ops->read)(viewer, &(s[i++]), 1, PETSC_CHAR);CHKERRQ(ierr);} 385 while (s[i-1]!='\n' && s[i-1]!='\t' && s[i-1]!=' ' && s[i-1]!='\0'); 386 /* Terminate final string */ 387 if (c == count-1) s[i-1] = '\0'; 388 } 389 } else { 390 ierr = (*viewer->ops->read)(viewer, data, count, dtype);CHKERRQ(ierr); 391 } 392 PetscFunctionReturn(0); 393 } 394