1 #define PETSC_DLL 2 /* 3 These routines simplify the use of command line, file options, etc., 4 and are used to manipulate the options database. 5 6 This file uses regular malloc and free because it cannot know 7 what malloc is being used until it has already processed the input. 8 */ 9 10 #include "petsc.h" /*I "petsc.h" I*/ 11 #include "petscsys.h" 12 #if defined(PETSC_HAVE_STDLIB_H) 13 #include <stdlib.h> 14 #endif 15 #if defined(PETSC_HAVE_MALLOC_H) 16 #include <malloc.h> 17 #endif 18 #if defined(PETSC_HAVE_SYS_PARAM_H) 19 #include "sys/param.h" 20 #endif 21 #include "petscfix.h" 22 23 /* 24 For simplicity, we use a static size database 25 */ 26 #define MAXOPTIONS 512 27 #define MAXALIASES 25 28 #define MAXOPTIONSMONITORS 5 29 30 typedef struct { 31 int N,argc,Naliases; 32 char **args,*names[MAXOPTIONS],*values[MAXOPTIONS]; 33 char *aliases1[MAXALIASES],*aliases2[MAXALIASES]; 34 PetscTruth used[MAXOPTIONS]; 35 PetscTruth namegiven; 36 char programname[PETSC_MAX_PATH_LEN]; /* HP includes entire path in name */ 37 38 /* --------User (or default) routines (most return -1 on error) --------*/ 39 PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], void*); /* returns control to user after */ 40 PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void*); /* */ 41 void *monitorcontext[MAXOPTIONSMONITORS]; /* to pass arbitrary user data into monitor */ 42 PetscInt numbermonitors; /* to, for instance, detect options being set */ 43 44 } PetscOptionsTable; 45 46 47 static PetscOptionsTable *options = 0; 48 49 extern PetscOptionsObjectType PetscOptionsObject; 50 51 /* 52 Options events monitor 53 */ 54 #define PetscOptionsMonitor(name,value) \ 55 { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \ 56 for (_i=0; _i<_im; _i++) {\ 57 _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \ 58 } \ 59 } 60 61 #undef __FUNCT__ 62 #define __FUNCT__ "PetscOptionsAtoi" 63 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtoi(const char name[],PetscInt *a) 64 { 65 PetscErrorCode ierr; 66 size_t i,len; 67 PetscTruth decide,tdefault,mouse; 68 69 PetscFunctionBegin; 70 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 71 if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 72 73 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr); 74 if (!tdefault) { 75 ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr); 76 } 77 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr); 78 if (!decide) { 79 ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr); 80 } 81 ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr); 82 83 if (tdefault) { 84 *a = PETSC_DEFAULT; 85 } else if (decide) { 86 *a = PETSC_DECIDE; 87 } else if (mouse) { 88 *a = -1; 89 } else { 90 if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') { 91 SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name); 92 } 93 for (i=1; i<len; i++) { 94 if (name[i] < '0' || name[i] > '9') { 95 SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name); 96 } 97 } 98 *a = atoi(name); 99 } 100 PetscFunctionReturn(0); 101 } 102 103 #undef __FUNCT__ 104 #define __FUNCT__ "PetscOptionsAtod" 105 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtod(const char name[],PetscReal *a) 106 { 107 PetscErrorCode ierr; 108 size_t len; 109 PetscTruth decide,tdefault; 110 111 PetscFunctionBegin; 112 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 113 if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 114 115 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr); 116 if (!tdefault) { 117 ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr); 118 } 119 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr); 120 if (!decide) { 121 ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr); 122 } 123 124 if (tdefault) { 125 *a = PETSC_DEFAULT; 126 } else if (decide) { 127 *a = PETSC_DECIDE; 128 } else { 129 if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') { 130 SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name); 131 } 132 *a = atof(name); 133 } 134 PetscFunctionReturn(0); 135 } 136 137 #undef __FUNCT__ 138 #define __FUNCT__ "PetscGetProgramName" 139 /*@C 140 PetscGetProgramName - Gets the name of the running program. 141 142 Not Collective 143 144 Input Parameter: 145 . len - length of the string name 146 147 Output Parameter: 148 . name - the name of the running program 149 150 Level: advanced 151 152 Notes: 153 The name of the program is copied into the user-provided character 154 array of length len. On some machines the program name includes 155 its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN. 156 @*/ 157 PetscErrorCode PETSC_DLLEXPORT PetscGetProgramName(char name[],size_t len) 158 { 159 PetscErrorCode ierr; 160 161 PetscFunctionBegin; 162 if (!options) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call PetscInitialize() first"); 163 if (!options->namegiven) SETERRQ(PETSC_ERR_PLIB,"Unable to determine program name"); 164 ierr = PetscStrncpy(name,options->programname,len);CHKERRQ(ierr); 165 PetscFunctionReturn(0); 166 } 167 168 #undef __FUNCT__ 169 #define __FUNCT__ "PetscSetProgramName" 170 PetscErrorCode PETSC_DLLEXPORT PetscSetProgramName(const char name[]) 171 { 172 PetscErrorCode ierr; 173 174 PetscFunctionBegin; 175 options->namegiven = PETSC_TRUE; 176 ierr = PetscStrncpy(options->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 177 PetscFunctionReturn(0); 178 } 179 180 #undef __FUNCT__ 181 #define __FUNCT__ "PetscOptionsValidKey" 182 PetscErrorCode PETSC_DLLEXPORT PetscOptionsValidKey(const char in_str[],PetscTruth *key) 183 { 184 PetscFunctionBegin; 185 *key = PETSC_FALSE; 186 if (!in_str) PetscFunctionReturn(0); 187 if (in_str[0] != '-') PetscFunctionReturn(0); 188 if ((in_str[1] < 'A') || (in_str[1] > 'z')) PetscFunctionReturn(0); 189 *key = PETSC_TRUE; 190 PetscFunctionReturn(0); 191 } 192 193 #undef __FUNCT__ 194 #define __FUNCT__ "PetscOptionsInsertString" 195 /*@C 196 PetscOptionsInsertString - Inserts options into the database from a string 197 198 Not collective: but only processes that call this routine will set the options 199 included in the file 200 201 Input Parameter: 202 . in_str - string that contains options separated by blanks 203 204 205 Level: intermediate 206 207 Contributed by Boyana Norris 208 209 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(), 210 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 211 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 212 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 213 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 214 PetscOptionsList(), PetscOptionsEList(), PetscOptionsInsertFile() 215 216 @*/ 217 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertString(const char in_str[]) 218 { 219 char *first,*second; 220 PetscErrorCode ierr; 221 PetscToken token; 222 PetscTruth key; 223 224 PetscFunctionBegin; 225 ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr); 226 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 227 while (first) { 228 ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr); 229 if (key) { 230 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 231 ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr); 232 if (!key) { 233 ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr); 234 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 235 } else { 236 ierr = PetscOptionsSetValue(first,PETSC_NULL);CHKERRQ(ierr); 237 first = second; 238 } 239 } else { 240 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 241 } 242 } 243 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 244 PetscFunctionReturn(0); 245 } 246 247 #undef __FUNCT__ 248 #define __FUNCT__ "PetscOptionsInsertFile" 249 /*@C 250 PetscOptionsInsertFile - Inserts options into the database from a file. 251 252 Not collective: but only processes that call this routine will set the options 253 included in the file 254 255 Input Parameter: 256 . file - name of file 257 258 259 Level: intermediate 260 261 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(), 262 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 263 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 264 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 265 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 266 PetscOptionsList(), PetscOptionsEList() 267 268 @*/ 269 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertFile(const char file[]) 270 { 271 char string[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],*first,*second,*third; 272 PetscErrorCode ierr; 273 size_t i,len,startIndex; 274 FILE *fd; 275 PetscToken token; 276 int err; 277 278 PetscFunctionBegin; 279 ierr = PetscFixFilename(file,fname);CHKERRQ(ierr); 280 fd = fopen(fname,"r"); 281 if (fd) { 282 while (fgets(string,128,fd)) { 283 /* Comments are indicated by #, ! or % in the first column */ 284 if (string[0] == '#') continue; 285 if (string[0] == '!') continue; 286 if (string[0] == '%') continue; 287 288 ierr = PetscStrlen(string,&len);CHKERRQ(ierr); 289 290 /* replace tabs, ^M, \n with " " */ 291 for (i=0; i<len; i++) { 292 if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') { 293 string[i] = ' '; 294 } 295 } 296 for(startIndex = 0; startIndex < len-1; startIndex++) { 297 if (string[startIndex] != ' ') break; 298 } 299 ierr = PetscTokenCreate(&string[startIndex],' ',&token);CHKERRQ(ierr); 300 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 301 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 302 if (first && first[0] == '-') { 303 ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr); 304 } else if (first) { 305 PetscTruth match; 306 307 ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr); 308 if (match) { 309 ierr = PetscTokenFind(token,&third);CHKERRQ(ierr); 310 if (!third) SETERRQ1(PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second); 311 ierr = PetscOptionsSetAlias(second,third);CHKERRQ(ierr); 312 } 313 } 314 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 315 } 316 err = fclose(fd); 317 if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file"); 318 } else { 319 SETERRQ1(PETSC_ERR_USER,"Unable to open Options File %s",fname); 320 } 321 PetscFunctionReturn(0); 322 } 323 324 #undef __FUNCT__ 325 #define __FUNCT__ "PetscOptionsInsert" 326 /*@C 327 PetscOptionsInsert - Inserts into the options database from the command line, 328 the environmental variable and a file. 329 330 Input Parameters: 331 + argc - count of number of command line arguments 332 . args - the command line arguments 333 - file - optional filename, defaults to ~username/.petscrc 334 335 Note: 336 Since PetscOptionsInsert() is automatically called by PetscInitialize(), 337 the user does not typically need to call this routine. PetscOptionsInsert() 338 can be called several times, adding additional entries into the database. 339 340 Options Database Keys: 341 + -options_monitor <optional filename> - print options names and values as they are set 342 343 Level: advanced 344 345 Concepts: options database^adding 346 347 .seealso: PetscOptionsDestroy_Private(), PetscOptionsPrint(), PetscOptionsInsertString(), PetscOptionsInsertFile(), 348 PetscInitialize() 349 @*/ 350 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsert(int *argc,char ***args,const char file[]) 351 { 352 PetscErrorCode ierr; 353 PetscMPIInt rank; 354 char pfile[PETSC_MAX_PATH_LEN]; 355 PetscTruth flag; 356 357 PetscFunctionBegin; 358 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 359 360 options->argc = (argc) ? *argc : 0; 361 options->args = (args) ? *args : PETSC_NULL; 362 363 if (file) { 364 ierr = PetscOptionsInsertFile(file);CHKERRQ(ierr); 365 } 366 ierr = PetscOptionsHasName(PETSC_NULL,"-skip_petscrc",&flag);CHKERRQ(ierr); 367 if (!flag) { 368 ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr); 369 if (pfile[0]) { 370 ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr); 371 ierr = PetscTestFile(pfile,'r',&flag);CHKERRQ(ierr); 372 if (flag) { 373 ierr = PetscOptionsInsertFile(pfile);CHKERRQ(ierr); 374 ierr = PetscInfo(0,"Loading ~/.petscrc\n");CHKERRQ(ierr); 375 } 376 } 377 ierr = PetscTestFile(".petscrc",'r',&flag);CHKERRQ(ierr); 378 if (flag) { 379 ierr = PetscOptionsInsertFile(".petscrc");CHKERRQ(ierr); 380 ierr = PetscInfo(0,"Loading local directory file .petscrc\n");CHKERRQ(ierr); 381 } 382 } 383 384 /* insert environmental options */ 385 { 386 char *eoptions = 0; 387 size_t len = 0; 388 if (!rank) { 389 eoptions = (char*)getenv("PETSC_OPTIONS"); 390 ierr = PetscStrlen(eoptions,&len);CHKERRQ(ierr); 391 ierr = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 392 } else { 393 ierr = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 394 if (len) { 395 ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr); 396 } 397 } 398 if (len) { 399 ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 400 if (rank) eoptions[len] = 0; 401 ierr = PetscOptionsInsertString(eoptions);CHKERRQ(ierr); 402 if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);} 403 } 404 } 405 406 /* insert command line options */ 407 if (argc && args && *argc) { 408 int left = *argc - 1; 409 char **eargs = *args + 1; 410 PetscTruth isoptions_file,isp4,tisp4,isp4yourname,isp4rmrank; 411 412 while (left) { 413 ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr); 414 ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr); 415 ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr); 416 ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr); 417 ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr); 418 isp4 = (PetscTruth) (isp4 || tisp4); 419 ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr); 420 isp4 = (PetscTruth) (isp4 || tisp4); 421 ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr); 422 423 if (eargs[0][0] != '-') { 424 eargs++; left--; 425 } else if (isoptions_file) { 426 if (left <= 1) SETERRQ(PETSC_ERR_USER,"Missing filename for -options_file filename option"); 427 if (eargs[1][0] == '-') SETERRQ(PETSC_ERR_USER,"Missing filename for -options_file filename option"); 428 ierr = PetscOptionsInsertFile(eargs[1]);CHKERRQ(ierr); 429 eargs += 2; left -= 2; 430 431 /* 432 These are "bad" options that MPICH, etc put on the command line 433 we strip them out here. 434 */ 435 } else if (tisp4 || isp4rmrank) { 436 eargs += 1; left -= 1; 437 } else if (isp4 || isp4yourname) { 438 eargs += 2; left -= 2; 439 } else if ((left < 2) || ((eargs[1][0] == '-') && 440 ((eargs[1][1] > '9') || (eargs[1][1] < '0')))) { 441 ierr = PetscOptionsSetValue(eargs[0],PETSC_NULL);CHKERRQ(ierr); 442 eargs++; left--; 443 } else { 444 ierr = PetscOptionsSetValue(eargs[0],eargs[1]);CHKERRQ(ierr); 445 eargs += 2; left -= 2; 446 } 447 } 448 } 449 PetscFunctionReturn(0); 450 } 451 452 #undef __FUNCT__ 453 #define __FUNCT__ "PetscOptionsPrint" 454 /*@C 455 PetscOptionsPrint - Prints the options that have been loaded. This is 456 useful for debugging purposes. 457 458 Collective on PETSC_COMM_WORLD 459 460 Input Parameter: 461 . FILE fd - location to print options (usually stdout or stderr) 462 463 Options Database Key: 464 . -optionstable - Activates PetscOptionsPrint() within PetscFinalize() 465 466 Level: advanced 467 468 Concepts: options database^printing 469 470 .seealso: PetscOptionsAllUsed() 471 @*/ 472 PetscErrorCode PETSC_DLLEXPORT PetscOptionsPrint(FILE *fd) 473 { 474 PetscErrorCode ierr; 475 PetscInt i; 476 477 PetscFunctionBegin; 478 if (!fd) fd = PETSC_STDOUT; 479 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 480 for (i=0; i<options->N; i++) { 481 if (options->values[i]) { 482 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr); 483 } else { 484 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s\n",options->names[i]);CHKERRQ(ierr); 485 } 486 } 487 PetscFunctionReturn(0); 488 } 489 490 #undef __FUNCT__ 491 #define __FUNCT__ "PetscOptionsGetAll" 492 /*@C 493 PetscOptionsGetAll - Lists all the options the program was run with in a single string. 494 495 Not Collective 496 497 Output Parameter: 498 . copts - pointer where string pointer is stored 499 500 Level: advanced 501 502 Concepts: options database^listing 503 504 .seealso: PetscOptionsAllUsed(), PetscOptionsPrint() 505 @*/ 506 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetAll(char *copts[]) 507 { 508 PetscErrorCode ierr; 509 PetscInt i; 510 size_t len = 1,lent; 511 char *coptions; 512 513 PetscFunctionBegin; 514 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 515 516 /* count the length of the required string */ 517 for (i=0; i<options->N; i++) { 518 ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr); 519 len += 2 + lent; 520 if (options->values[i]) { 521 ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr); 522 len += 1 + lent; 523 } 524 } 525 ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr); 526 coptions[0] = 0; 527 for (i=0; i<options->N; i++) { 528 ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr); 529 ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr); 530 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 531 if (options->values[i]) { 532 ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr); 533 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 534 } 535 } 536 *copts = coptions; 537 PetscFunctionReturn(0); 538 } 539 540 #undef __FUNCT__ 541 #define __FUNCT__ "PetscOptionsDestroy" 542 /*@C 543 PetscOptionsDestroy - Destroys the option database. 544 545 Note: 546 Since PetscOptionsDestroy() is called by PetscFinalize(), the user 547 typically does not need to call this routine. 548 549 Level: developer 550 551 .seealso: PetscOptionsInsert() 552 @*/ 553 PetscErrorCode PETSC_DLLEXPORT PetscOptionsDestroy(void) 554 { 555 PetscInt i; 556 557 PetscFunctionBegin; 558 if (!options) PetscFunctionReturn(0); 559 for (i=0; i<options->N; i++) { 560 if (options->names[i]) free(options->names[i]); 561 if (options->values[i]) free(options->values[i]); 562 } 563 for (i=0; i<options->Naliases; i++) { 564 free(options->aliases1[i]); 565 free(options->aliases2[i]); 566 } 567 free(options); 568 options = 0; 569 PetscFunctionReturn(0); 570 } 571 572 #undef __FUNCT__ 573 #define __FUNCT__ "PetscOptionsSetValue" 574 /*@C 575 PetscOptionsSetValue - Sets an option name-value pair in the options 576 database, overriding whatever is already present. 577 578 Not collective, but setting values on certain processors could cause problems 579 for parallel objects looking for options. 580 581 Input Parameters: 582 + name - name of option, this SHOULD have the - prepended 583 - value - the option value (not used for all options) 584 585 Level: intermediate 586 587 Note: 588 Only some options have values associated with them, such as 589 -ksp_rtol tol. Other options stand alone, such as -ksp_monitor. 590 591 Concepts: options database^adding option 592 593 .seealso: PetscOptionsInsert() 594 @*/ 595 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetValue(const char iname[],const char value[]) 596 { 597 size_t len; 598 PetscErrorCode ierr; 599 PetscInt N,n,i; 600 char **names; 601 const char *name = (char*)iname; 602 PetscTruth gt,match; 603 604 PetscFunctionBegin; 605 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 606 607 /* this is so that -h and -help are equivalent (p4 does not like -help)*/ 608 ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr); 609 if (match) name = "-help"; 610 611 name++; 612 /* first check against aliases */ 613 N = options->Naliases; 614 for (i=0; i<N; i++) { 615 ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr); 616 if (match) { 617 name = options->aliases2[i]; 618 break; 619 } 620 } 621 622 N = options->N; 623 n = N; 624 names = options->names; 625 626 for (i=0; i<N; i++) { 627 ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr); 628 ierr = PetscStrgrt(names[i],name,>);CHKERRQ(ierr); 629 if (match) { 630 if (options->values[i]) free(options->values[i]); 631 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 632 if (len) { 633 options->values[i] = (char*)malloc((len+1)*sizeof(char)); 634 ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr); 635 } else { options->values[i] = 0;} 636 PetscOptionsMonitor(name,value); 637 PetscFunctionReturn(0); 638 } else if (gt) { 639 n = i; 640 break; 641 } 642 } 643 if (N >= MAXOPTIONS) { 644 SETERRQ1(PETSC_ERR_PLIB,"No more room in option table, limit %d recompile \n src/sys/objects/options.c with larger value for MAXOPTIONS\n",MAXOPTIONS); 645 } 646 /* shift remaining values down 1 */ 647 for (i=N; i>n; i--) { 648 names[i] = names[i-1]; 649 options->values[i] = options->values[i-1]; 650 options->used[i] = options->used[i-1]; 651 } 652 /* insert new name and value */ 653 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 654 names[n] = (char*)malloc((len+1)*sizeof(char)); 655 ierr = PetscStrcpy(names[n],name);CHKERRQ(ierr); 656 if (value) { 657 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 658 options->values[n] = (char*)malloc((len+1)*sizeof(char)); 659 ierr = PetscStrcpy(options->values[n],value);CHKERRQ(ierr); 660 } else {options->values[n] = 0;} 661 options->used[n] = PETSC_FALSE; 662 options->N++; 663 PetscOptionsMonitor(name,value); 664 PetscFunctionReturn(0); 665 } 666 667 #undef __FUNCT__ 668 #define __FUNCT__ "PetscOptionsClearValue" 669 /*@C 670 PetscOptionsClearValue - Clears an option name-value pair in the options 671 database, overriding whatever is already present. 672 673 Not Collective, but setting values on certain processors could cause problems 674 for parallel objects looking for options. 675 676 Input Parameter: 677 . name - name of option, this SHOULD have the - prepended 678 679 Level: intermediate 680 681 Concepts: options database^removing option 682 .seealso: PetscOptionsInsert() 683 @*/ 684 PetscErrorCode PETSC_DLLEXPORT PetscOptionsClearValue(const char iname[]) 685 { 686 PetscErrorCode ierr; 687 PetscInt N,n,i; 688 char **names,*name=(char*)iname; 689 PetscTruth gt,match; 690 691 PetscFunctionBegin; 692 if (name[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name); 693 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 694 695 name++; 696 697 N = options->N; n = 0; 698 names = options->names; 699 700 for (i=0; i<N; i++) { 701 ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr); 702 ierr = PetscStrgrt(names[i],name,>);CHKERRQ(ierr); 703 if (match) { 704 if (options->values[i]) free(options->values[i]); 705 PetscOptionsMonitor(name,""); 706 break; 707 } else if (gt) { 708 PetscFunctionReturn(0); /* it was not listed */ 709 } 710 n++; 711 } 712 if (n == N) PetscFunctionReturn(0); /* it was not listed */ 713 714 /* shift remaining values down 1 */ 715 for (i=n; i<N-1; i++) { 716 names[i] = names[i+1]; 717 options->values[i] = options->values[i+1]; 718 options->used[i] = options->used[i+1]; 719 } 720 options->N--; 721 PetscFunctionReturn(0); 722 } 723 724 #undef __FUNCT__ 725 #define __FUNCT__ "PetscOptionsSetAlias" 726 /*@C 727 PetscOptionsReject - Generates an error if a certain option is given. 728 729 Not Collective, but setting values on certain processors could cause problems 730 for parallel objects looking for options. 731 732 Input Parameters: 733 + name - the option one is seeking 734 - mess - error message (may be PETSC_NULL) 735 736 Level: advanced 737 738 Concepts: options database^rejecting option 739 740 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 741 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(), 742 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 743 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 744 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 745 PetscOptionsList(), PetscOptionsEList() 746 @*/ 747 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetAlias(const char inewname[],const char ioldname[]) 748 { 749 PetscErrorCode ierr; 750 PetscInt n = options->Naliases; 751 size_t len; 752 char *newname = (char *)inewname,*oldname = (char*)ioldname; 753 754 PetscFunctionBegin; 755 if (newname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname); 756 if (oldname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname); 757 if (n >= MAXALIASES) { 758 SETERRQ1(PETSC_ERR_MEM,"You have defined to many PETSc options aliases, limit %d recompile \n src/sys/objects/options.c with larger value for MAXALIASES",MAXALIASES); 759 } 760 761 newname++; oldname++; 762 ierr = PetscStrlen(newname,&len);CHKERRQ(ierr); 763 options->aliases1[n] = (char*)malloc((len+1)*sizeof(char)); 764 ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr); 765 ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr); 766 options->aliases2[n] = (char*)malloc((len+1)*sizeof(char)); 767 ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr); 768 options->Naliases++; 769 PetscFunctionReturn(0); 770 } 771 772 #undef __FUNCT__ 773 #define __FUNCT__ "PetscOptionsFindPair_Private" 774 static PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscTruth *flg) 775 { 776 PetscErrorCode ierr; 777 PetscInt i,N; 778 size_t len; 779 char **names,tmp[256]; 780 PetscTruth match; 781 782 PetscFunctionBegin; 783 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 784 N = options->N; 785 names = options->names; 786 787 if (name[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name); 788 789 /* append prefix to name */ 790 if (pre) { 791 if (pre[0] == '-') SETERRQ(PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -"); 792 ierr = PetscStrncpy(tmp,pre,256);CHKERRQ(ierr); 793 ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr); 794 ierr = PetscStrncat(tmp,name+1,256-len-1);CHKERRQ(ierr); 795 } else { 796 ierr = PetscStrncpy(tmp,name+1,256);CHKERRQ(ierr); 797 } 798 799 /* slow search */ 800 *flg = PETSC_FALSE; 801 for (i=0; i<N; i++) { 802 ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr); 803 if (match) { 804 *value = options->values[i]; 805 options->used[i] = PETSC_TRUE; 806 *flg = PETSC_TRUE; 807 break; 808 } 809 } 810 if (!*flg) { 811 PetscInt j,cnt = 0,locs[16],loce[16]; 812 size_t n; 813 ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr); 814 /* determine the location and number of all _%d_ in the key */ 815 for (i=0; i< (PetscInt)n; i++) { 816 if (tmp[i] == '_') { 817 for (j=i+1; j< (PetscInt)n; j++) { 818 if (tmp[j] >= '0' && tmp[j] <= '9') continue; 819 if (tmp[j] == '_' && j > i+1) { /* found a number */ 820 locs[cnt] = i+1; 821 loce[cnt++] = j+1; 822 } 823 break; 824 } 825 } 826 } 827 if (cnt) { 828 char tmp2[256]; 829 for (i=0; i<cnt; i++) { 830 ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr); 831 ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr); 832 ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr); 833 ierr = PetscOptionsFindPair_Private(PETSC_NULL,tmp2,value,flg);CHKERRQ(ierr); 834 if (*flg) break; 835 } 836 } 837 } 838 PetscFunctionReturn(0); 839 } 840 841 #undef __FUNCT__ 842 #define __FUNCT__ "PetscOptionsReject" 843 /*@C 844 PetscOptionsReject - Generates an error if a certain option is given. 845 846 Not Collective, but setting values on certain processors could cause problems 847 for parallel objects looking for options. 848 849 Input Parameters: 850 + name - the option one is seeking 851 - mess - error message (may be PETSC_NULL) 852 853 Level: advanced 854 855 Concepts: options database^rejecting option 856 857 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 858 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 859 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 860 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 861 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 862 PetscOptionsList(), PetscOptionsEList() 863 @*/ 864 PetscErrorCode PETSC_DLLEXPORT PetscOptionsReject(const char name[],const char mess[]) 865 { 866 PetscErrorCode ierr; 867 PetscTruth flag; 868 869 PetscFunctionBegin; 870 ierr = PetscOptionsHasName(PETSC_NULL,name,&flag);CHKERRQ(ierr); 871 if (flag) { 872 if (mess) { 873 SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess); 874 } else { 875 SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name); 876 } 877 } 878 PetscFunctionReturn(0); 879 } 880 881 #undef __FUNCT__ 882 #define __FUNCT__ "PetscOptionsHasName" 883 /*@C 884 PetscOptionsHasName - Determines whether a certain option is given in the database. 885 886 Not Collective 887 888 Input Parameters: 889 + name - the option one is seeking 890 - pre - string to prepend to the name or PETSC_NULL 891 892 Output Parameters: 893 . flg - PETSC_TRUE if found else PETSC_FALSE. 894 895 Level: beginner 896 897 Concepts: options database^has option name 898 899 Notes: Name cannot be simply -h 900 901 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 902 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 903 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 904 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 905 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 906 PetscOptionsList(), PetscOptionsEList() 907 @*/ 908 PetscErrorCode PETSC_DLLEXPORT PetscOptionsHasName(const char pre[],const char name[],PetscTruth *flg) 909 { 910 char *value; 911 PetscErrorCode ierr; 912 PetscTruth isfalse,flag; 913 914 PetscFunctionBegin; 915 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 916 917 /* remove if turned off */ 918 if (flag) { 919 ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr); 920 if (isfalse) flag = PETSC_FALSE; 921 ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr); 922 if (isfalse) flag = PETSC_FALSE; 923 ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr); 924 if (isfalse) flag = PETSC_FALSE; 925 } 926 if (flg) *flg = flag; 927 PetscFunctionReturn(0); 928 } 929 930 #undef __FUNCT__ 931 #define __FUNCT__ "PetscOptionsGetInt" 932 /*@C 933 PetscOptionsGetInt - Gets the integer value for a particular option in the database. 934 935 Not Collective 936 937 Input Parameters: 938 + pre - the string to prepend to the name or PETSC_NULL 939 - name - the option one is seeking 940 941 Output Parameter: 942 + ivalue - the integer value to return 943 - flg - PETSC_TRUE if found, else PETSC_FALSE 944 945 Level: beginner 946 947 Concepts: options database^has int 948 949 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 950 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth() 951 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(), 952 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 953 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 954 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 955 PetscOptionsList(), PetscOptionsEList() 956 @*/ 957 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscTruth *flg) 958 { 959 char *value; 960 PetscErrorCode ierr; 961 PetscTruth flag; 962 963 PetscFunctionBegin; 964 PetscValidCharPointer(name,2); 965 PetscValidIntPointer(ivalue,3); 966 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 967 if (flag) { 968 if (!value) {if (flg) *flg = PETSC_FALSE;} 969 else { 970 if (flg) *flg = PETSC_TRUE; 971 ierr = PetscOptionsAtoi(value,ivalue);CHKERRQ(ierr); 972 } 973 } else { 974 if (flg) *flg = PETSC_FALSE; 975 } 976 PetscFunctionReturn(0); 977 } 978 979 #undef __FUNCT__ 980 #define __FUNCT__ "PetscOptionsGetEList" 981 /*@C 982 PetscOptionsGetEList - Puts a list of option values that a single one may be selected from 983 984 Not Collective 985 986 Input Parameters: 987 + pre - the string to prepend to the name or PETSC_NULL 988 . opt - option name 989 . list - the possible choices 990 . ntext - number of choices 991 992 Output Parameter: 993 + value - the index of the value to return 994 - set - PETSC_TRUE if found, else PETSC_FALSE 995 996 Level: intermediate 997 998 See PetscOptionsList() for when the choices are given in a PetscFList() 999 1000 Concepts: options database^list 1001 1002 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1003 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1004 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1005 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1006 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1007 PetscOptionsList(), PetscOptionsEList() 1008 @*/ 1009 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEList(const char pre[],const char opt[],const char **list,PetscInt ntext,PetscInt *value,PetscTruth *set) 1010 { 1011 PetscErrorCode ierr; 1012 size_t alen,len = 0; 1013 char *svalue; 1014 PetscTruth aset,flg = PETSC_FALSE; 1015 PetscInt i; 1016 1017 PetscFunctionBegin; 1018 for ( i=0; i<ntext; i++) { 1019 ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr); 1020 if (alen > len) len = alen; 1021 } 1022 len += 5; /* a little extra space for user mistypes */ 1023 ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr); 1024 ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr); 1025 if (aset) { 1026 if (set) *set = PETSC_TRUE; 1027 for (i=0; i<ntext; i++) { 1028 ierr = PetscStrcasecmp(svalue,list[i],&flg);CHKERRQ(ierr); 1029 if (flg) { 1030 *value = i; 1031 break; 1032 } 1033 } 1034 if (!flg) SETERRQ3(PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre?pre:"",opt+1); 1035 } else if (set) { 1036 *set = PETSC_FALSE; 1037 } 1038 ierr = PetscFree(svalue);CHKERRQ(ierr); 1039 PetscFunctionReturn(0); 1040 } 1041 1042 #undef __FUNCT__ 1043 #define __FUNCT__ "PetscOptionsEnum" 1044 /*@C 1045 PetscOptionsGetEnum - Gets the enum value for a particular option in the database. 1046 1047 Not Collective 1048 1049 Input Parameters: 1050 + pre - option prefix or PETSC_NULL 1051 . opt - option name 1052 . list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null 1053 - defaultv - the default (current) value 1054 1055 Output Parameter: 1056 + value - the value to return 1057 - flg - PETSC_TRUE if found, else PETSC_FALSE 1058 1059 Level: beginner 1060 1061 Concepts: options database 1062 1063 Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd() 1064 1065 list is usually something like PCASMTypes or some other predefined list of enum names 1066 1067 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(), 1068 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth() 1069 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(), 1070 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1071 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1072 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1073 PetscOptionsList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum() 1074 @*/ 1075 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEnum(const char pre[],const char opt[],const char **list,PetscEnum *value,PetscTruth *set) 1076 { 1077 PetscErrorCode ierr; 1078 PetscInt ntext = 0; 1079 1080 PetscFunctionBegin; 1081 while (list[ntext++]) { 1082 if (ntext > 50) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries"); 1083 } 1084 if (ntext < 3) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix"); 1085 ntext -= 3; 1086 ierr = PetscOptionsGetEList(pre,opt,list,ntext,(PetscInt*)value,set);CHKERRQ(ierr); 1087 PetscFunctionReturn(0); 1088 } 1089 1090 #undef __FUNCT__ 1091 #define __FUNCT__ "PetscOptionsGetTruth" 1092 /*@C 1093 PetscOptionsGetTruth - Gets the Logical (true or false) value for a particular 1094 option in the database. 1095 1096 Not Collective 1097 1098 Input Parameters: 1099 + pre - the string to prepend to the name or PETSC_NULL 1100 - name - the option one is seeking 1101 1102 Output Parameter: 1103 + ivalue - the logical value to return 1104 - flg - PETSC_TRUE if found, else PETSC_FALSE 1105 1106 Level: beginner 1107 1108 Notes: 1109 TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE 1110 FALSE, false, NO, no, and 0 all translate to PETSC_FALSE 1111 1112 Concepts: options database^has logical 1113 1114 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 1115 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsTruth(), 1116 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1117 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1118 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1119 PetscOptionsList(), PetscOptionsEList() 1120 @*/ 1121 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruth(const char pre[],const char name[],PetscTruth *ivalue,PetscTruth *flg) 1122 { 1123 char *value; 1124 PetscTruth flag,istrue,isfalse; 1125 PetscErrorCode ierr; 1126 1127 PetscFunctionBegin; 1128 PetscValidCharPointer(name,2); 1129 PetscValidIntPointer(ivalue,3); 1130 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1131 if (flag) { 1132 if (flg) *flg = PETSC_TRUE; 1133 if (!value) { 1134 *ivalue = PETSC_TRUE; 1135 } else { 1136 *ivalue = PETSC_TRUE; 1137 ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr); 1138 if (istrue) PetscFunctionReturn(0); 1139 ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr); 1140 if (istrue) PetscFunctionReturn(0); 1141 ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr); 1142 if (istrue) PetscFunctionReturn(0); 1143 ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr); 1144 if (istrue) PetscFunctionReturn(0); 1145 1146 *ivalue = PETSC_FALSE; 1147 ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr); 1148 if (isfalse) PetscFunctionReturn(0); 1149 ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr); 1150 if (isfalse) PetscFunctionReturn(0); 1151 ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr); 1152 if (isfalse) PetscFunctionReturn(0); 1153 ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr); 1154 if (isfalse) PetscFunctionReturn(0); 1155 1156 SETERRQ1(PETSC_ERR_ARG_WRONG,"Unknown logical value: %s",value); 1157 } 1158 } else { 1159 if (flg) *flg = PETSC_FALSE; 1160 } 1161 PetscFunctionReturn(0); 1162 } 1163 1164 #undef __FUNCT__ 1165 #define __FUNCT__ "PetscOptionsGetReal" 1166 /*@C 1167 PetscOptionsGetReal - Gets the double precision value for a particular 1168 option in the database. 1169 1170 Not Collective 1171 1172 Input Parameters: 1173 + pre - string to prepend to each name or PETSC_NULL 1174 - name - the option one is seeking 1175 1176 Output Parameter: 1177 + dvalue - the double value to return 1178 - flg - PETSC_TRUE if found, PETSC_FALSE if not found 1179 1180 Level: beginner 1181 1182 Concepts: options database^has double 1183 1184 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1185 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(), 1186 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1187 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1188 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1189 PetscOptionsList(), PetscOptionsEList() 1190 @*/ 1191 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscTruth *flg) 1192 { 1193 char *value; 1194 PetscErrorCode ierr; 1195 PetscTruth flag; 1196 1197 PetscFunctionBegin; 1198 PetscValidCharPointer(name,2); 1199 PetscValidDoublePointer(dvalue,3); 1200 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1201 if (flag) { 1202 if (!value) {if (flg) *flg = PETSC_FALSE;} 1203 else {if (flg) *flg = PETSC_TRUE; ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);} 1204 } else { 1205 if (flg) *flg = PETSC_FALSE; 1206 } 1207 PetscFunctionReturn(0); 1208 } 1209 1210 #undef __FUNCT__ 1211 #define __FUNCT__ "PetscOptionsGetScalar" 1212 /*@C 1213 PetscOptionsGetScalar - Gets the scalar value for a particular 1214 option in the database. 1215 1216 Not Collective 1217 1218 Input Parameters: 1219 + pre - string to prepend to each name or PETSC_NULL 1220 - name - the option one is seeking 1221 1222 Output Parameter: 1223 + dvalue - the double value to return 1224 - flg - PETSC_TRUE if found, else PETSC_FALSE 1225 1226 Level: beginner 1227 1228 Usage: 1229 A complex number 2+3i can be specified as 2,3 at the command line. 1230 or a number 2.0e-10 - 3.3e-20 i can be specified as 2.0e-10,3.3e-20 1231 1232 Concepts: options database^has scalar 1233 1234 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1235 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1236 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1237 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1238 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1239 PetscOptionsList(), PetscOptionsEList() 1240 @*/ 1241 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscTruth *flg) 1242 { 1243 char *value; 1244 PetscTruth flag; 1245 PetscErrorCode ierr; 1246 1247 PetscFunctionBegin; 1248 PetscValidCharPointer(name,2); 1249 PetscValidScalarPointer(dvalue,3); 1250 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1251 if (flag) { 1252 if (!value) { 1253 if (flg) *flg = PETSC_FALSE; 1254 } else { 1255 #if !defined(PETSC_USE_COMPLEX) 1256 ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr); 1257 #else 1258 PetscReal re=0.0,im=0.0; 1259 PetscToken token; 1260 char *tvalue = 0; 1261 1262 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1263 ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr); 1264 if (!tvalue) { SETERRQ(PETSC_ERR_ARG_WRONG,"unknown string specified\n"); } 1265 ierr = PetscOptionsAtod(tvalue,&re);CHKERRQ(ierr); 1266 ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr); 1267 if (!tvalue) { /* Unknown separator used. using only real value */ 1268 *dvalue = re; 1269 } else { 1270 ierr = PetscOptionsAtod(tvalue,&im);CHKERRQ(ierr); 1271 *dvalue = re + PETSC_i*im; 1272 } 1273 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1274 #endif 1275 if (flg) *flg = PETSC_TRUE; 1276 } 1277 } else { /* flag */ 1278 if (flg) *flg = PETSC_FALSE; 1279 } 1280 PetscFunctionReturn(0); 1281 } 1282 1283 #undef __FUNCT__ 1284 #define __FUNCT__ "PetscOptionsGetRealArray" 1285 /*@C 1286 PetscOptionsGetRealArray - Gets an array of double precision values for a 1287 particular option in the database. The values must be separated with 1288 commas with no intervening spaces. 1289 1290 Not Collective 1291 1292 Input Parameters: 1293 + pre - string to prepend to each name or PETSC_NULL 1294 . name - the option one is seeking 1295 - nmax - maximum number of values to retrieve 1296 1297 Output Parameters: 1298 + dvalue - the double value to return 1299 . nmax - actual number of values retreived 1300 - flg - PETSC_TRUE if found, else PETSC_FALSE 1301 1302 Level: beginner 1303 1304 Concepts: options database^array of doubles 1305 1306 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1307 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 1308 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1309 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1310 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1311 PetscOptionsList(), PetscOptionsEList() 1312 @*/ 1313 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscTruth *flg) 1314 { 1315 char *value; 1316 PetscErrorCode ierr; 1317 PetscInt n = 0; 1318 PetscTruth flag; 1319 PetscToken token; 1320 1321 PetscFunctionBegin; 1322 PetscValidCharPointer(name,2); 1323 PetscValidDoublePointer(dvalue,3); 1324 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1325 if (!flag) {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 1326 if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);} 1327 1328 if (flg) *flg = PETSC_TRUE; 1329 1330 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1331 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1332 while (n < *nmax) { 1333 if (!value) break; 1334 ierr = PetscOptionsAtod(value,dvalue++);CHKERRQ(ierr); 1335 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1336 n++; 1337 } 1338 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1339 *nmax = n; 1340 PetscFunctionReturn(0); 1341 } 1342 1343 #undef __FUNCT__ 1344 #define __FUNCT__ "PetscOptionsGetIntArray" 1345 /*@C 1346 PetscOptionsGetIntArray - Gets an array of integer values for a particular 1347 option in the database. The values must be separated with commas with 1348 no intervening spaces. 1349 1350 Not Collective 1351 1352 Input Parameters: 1353 + pre - string to prepend to each name or PETSC_NULL 1354 . name - the option one is seeking 1355 - nmax - maximum number of values to retrieve 1356 1357 Output Parameter: 1358 + dvalue - the integer values to return 1359 . nmax - actual number of values retreived 1360 - flg - PETSC_TRUE if found, else PETSC_FALSE 1361 1362 Level: beginner 1363 1364 Concepts: options database^array of ints 1365 1366 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1367 PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1368 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1369 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1370 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1371 PetscOptionsList(), PetscOptionsEList() 1372 @*/ 1373 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscTruth *flg) 1374 { 1375 char *value; 1376 PetscErrorCode ierr; 1377 PetscInt n = 0,i,start,end; 1378 size_t len; 1379 PetscTruth flag,foundrange; 1380 PetscToken token; 1381 1382 PetscFunctionBegin; 1383 PetscValidCharPointer(name,2); 1384 PetscValidIntPointer(dvalue,3); 1385 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1386 if (!flag) {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 1387 if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);} 1388 1389 if (flg) *flg = PETSC_TRUE; 1390 1391 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1392 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1393 while (n < *nmax) { 1394 if (!value) break; 1395 1396 /* look for form d-D where d and D are integers */ 1397 foundrange = PETSC_FALSE; 1398 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 1399 if (value[0] == '-') i=2; 1400 else i=1; 1401 for (;i<(int)len; i++) { 1402 if (value[i] == '-') { 1403 if (i == (int)len-1) SETERRQ2(PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value); 1404 value[i] = 0; 1405 ierr = PetscOptionsAtoi(value,&start);CHKERRQ(ierr); 1406 ierr = PetscOptionsAtoi(value+i+1,&end);CHKERRQ(ierr); 1407 if (end <= start) SETERRQ3(PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1); 1408 if (n + end - start - 1 >= *nmax) SETERRQ4(PETSC_ERR_USER,"Error in %D-th array entry, not enough space in left in array (%D) to contain entire range from %D to %D",n,*nmax-n,start,end); 1409 for (;start<end; start++) { 1410 *dvalue = start; dvalue++;n++; 1411 } 1412 foundrange = PETSC_TRUE; 1413 break; 1414 } 1415 } 1416 if (!foundrange) { 1417 ierr = PetscOptionsAtoi(value,dvalue);CHKERRQ(ierr); 1418 dvalue++; 1419 n++; 1420 } 1421 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1422 } 1423 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1424 *nmax = n; 1425 PetscFunctionReturn(0); 1426 } 1427 1428 #undef __FUNCT__ 1429 #define __FUNCT__ "PetscOptionsGetString" 1430 /*@C 1431 PetscOptionsGetString - Gets the string value for a particular option in 1432 the database. 1433 1434 Not Collective 1435 1436 Input Parameters: 1437 + pre - string to prepend to name or PETSC_NULL 1438 . name - the option one is seeking 1439 - len - maximum string length 1440 1441 Output Parameters: 1442 + string - location to copy string 1443 - flg - PETSC_TRUE if found, else PETSC_FALSE 1444 1445 Level: beginner 1446 1447 Fortran Note: 1448 The Fortran interface is slightly different from the C/C++ 1449 interface (len is not used). Sample usage in Fortran follows 1450 .vb 1451 character *20 string 1452 integer flg, ierr 1453 call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-s',string,flg,ierr) 1454 .ve 1455 1456 Concepts: options database^string 1457 1458 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1459 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1460 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1461 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1462 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1463 PetscOptionsList(), PetscOptionsEList() 1464 @*/ 1465 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscTruth *flg) 1466 { 1467 char *value; 1468 PetscErrorCode ierr; 1469 PetscTruth flag; 1470 1471 PetscFunctionBegin; 1472 PetscValidCharPointer(name,2); 1473 PetscValidCharPointer(string,3); 1474 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1475 if (!flag) { 1476 if (flg) *flg = PETSC_FALSE; 1477 } else { 1478 if (flg) *flg = PETSC_TRUE; 1479 if (value) { 1480 ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr); 1481 } else { 1482 ierr = PetscMemzero(string,len);CHKERRQ(ierr); 1483 } 1484 } 1485 PetscFunctionReturn(0); 1486 } 1487 1488 #undef __FUNCT__ 1489 #define __FUNCT__ "PetscOptionsGetStringArray" 1490 /*@C 1491 PetscOptionsGetStringArray - Gets an array of string values for a particular 1492 option in the database. The values must be separated with commas with 1493 no intervening spaces. 1494 1495 Not Collective 1496 1497 Input Parameters: 1498 + pre - string to prepend to name or PETSC_NULL 1499 . name - the option one is seeking 1500 - nmax - maximum number of strings 1501 1502 Output Parameter: 1503 + strings - location to copy strings 1504 - flg - PETSC_TRUE if found, else PETSC_FALSE 1505 1506 Level: beginner 1507 1508 Notes: 1509 The user should pass in an array of pointers to char, to hold all the 1510 strings returned by this function. 1511 1512 The user is responsible for deallocating the strings that are 1513 returned. The Fortran interface for this routine is not supported. 1514 1515 Contributed by Matthew Knepley. 1516 1517 Concepts: options database^array of strings 1518 1519 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1520 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1521 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1522 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1523 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1524 PetscOptionsList(), PetscOptionsEList() 1525 @*/ 1526 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscTruth *flg) 1527 { 1528 char *value; 1529 PetscErrorCode ierr; 1530 PetscInt n; 1531 PetscTruth flag; 1532 PetscToken token; 1533 1534 PetscFunctionBegin; 1535 PetscValidCharPointer(name,2); 1536 PetscValidPointer(strings,3); 1537 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1538 if (!flag) {*nmax = 0; if (flg) *flg = PETSC_FALSE; PetscFunctionReturn(0);} 1539 if (!value) {*nmax = 0; if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);} 1540 if (!*nmax) {if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);} 1541 if (flg) *flg = PETSC_TRUE; 1542 1543 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1544 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1545 n = 0; 1546 while (n < *nmax) { 1547 if (!value) break; 1548 ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr); 1549 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1550 n++; 1551 } 1552 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1553 *nmax = n; 1554 PetscFunctionReturn(0); 1555 } 1556 1557 #undef __FUNCT__ 1558 #define __FUNCT__ "PetscOptionsAllUsed" 1559 /*@C 1560 PetscOptionsAllUsed - Returns a count of the number of options in the 1561 database that have never been selected. 1562 1563 Not Collective 1564 1565 Output Parameter: 1566 . N - count of options not used 1567 1568 Level: advanced 1569 1570 .seealso: PetscOptionsPrint() 1571 @*/ 1572 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAllUsed(int *N) 1573 { 1574 PetscInt i,n = 0; 1575 1576 PetscFunctionBegin; 1577 for (i=0; i<options->N; i++) { 1578 if (!options->used[i]) { n++; } 1579 } 1580 *N = n; 1581 PetscFunctionReturn(0); 1582 } 1583 1584 #undef __FUNCT__ 1585 #define __FUNCT__ "PetscOptionsLeft" 1586 /*@ 1587 PetscOptionsLeft - Prints to screen any options that were set and never used. 1588 1589 Not collective 1590 1591 Options Database Key: 1592 . -options_left - Activates OptionsAllUsed() within PetscFinalize() 1593 1594 Level: advanced 1595 1596 .seealso: PetscOptionsAllUsed() 1597 @*/ 1598 PetscErrorCode PETSC_DLLEXPORT PetscOptionsLeft(void) 1599 { 1600 PetscErrorCode ierr; 1601 PetscInt i; 1602 1603 PetscFunctionBegin; 1604 for (i=0; i<options->N; i++) { 1605 if (!options->used[i]) { 1606 if (options->values[i]) { 1607 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr); 1608 } else { 1609 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s no value \n",options->names[i]);CHKERRQ(ierr); 1610 } 1611 } 1612 } 1613 PetscFunctionReturn(0); 1614 } 1615 1616 1617 /* 1618 PetscOptionsCreate - Creates the empty options database. 1619 1620 */ 1621 #undef __FUNCT__ 1622 #define __FUNCT__ "PetscOptionsCreate" 1623 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCreate(void) 1624 { 1625 PetscErrorCode ierr; 1626 1627 PetscFunctionBegin; 1628 options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable)); 1629 ierr = PetscMemzero(options->used,MAXOPTIONS*sizeof(PetscTruth));CHKERRQ(ierr); 1630 options->namegiven = PETSC_FALSE; 1631 options->N = 0; 1632 options->Naliases = 0; 1633 options->numbermonitors = 0; 1634 1635 PetscOptionsObject.prefix = PETSC_NULL; 1636 PetscOptionsObject.title = PETSC_NULL; 1637 1638 PetscFunctionReturn(0); 1639 } 1640 1641 #undef __FUNCT__ 1642 #define __FUNCT__ "PetscOptionsSetFromOptions" 1643 /*@ 1644 PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options. 1645 1646 Collective on PETSC_COMM_WORLD 1647 1648 Options Database Keys: 1649 + -options_monitor <optional filename> - prints the names and values of all 1650 runtime options as they are set. The monitor functionality is not 1651 available for options set through a file, environment variable, or on 1652 the command line. Only options set after PetscInitialize completes will 1653 be monitored. 1654 . -options_monitor_cancel - cancel all options database monitors 1655 1656 Notes: 1657 To see all options, run your program with the -help option or consult 1658 the users manual. 1659 1660 Level: intermediate 1661 1662 .keywords: set, options, database 1663 @*/ 1664 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetFromOptions(void) 1665 { 1666 PetscTruth flg; 1667 PetscErrorCode ierr; 1668 char monfilename[PETSC_MAX_PATH_LEN]; 1669 PetscViewer monviewer; 1670 1671 PetscFunctionBegin; 1672 1673 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr); 1674 ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); 1675 if (flg && (!options->numbermonitors)) { 1676 ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr); 1677 ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 1678 } 1679 1680 ierr = PetscOptionsName("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",&flg);CHKERRQ(ierr); 1681 if (flg) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); } 1682 1683 ierr = PetscOptionsEnd();CHKERRQ(ierr); 1684 1685 PetscFunctionReturn(0); 1686 } 1687 1688 1689 #undef __FUNCT__ 1690 #define __FUNCT__ "PetscOptionsMonitorDefault" 1691 /*@C 1692 PetscOptionsMonitorDefault - Print all options set value events. 1693 1694 Collective on PETSC_COMM_WORLD 1695 1696 Input Parameters: 1697 + name - option name string 1698 . value - option value string 1699 - dummy - unused monitor context 1700 1701 Level: intermediate 1702 1703 .keywords: PetscOptions, default, monitor 1704 1705 .seealso: PetscOptionsMonitorSet() 1706 @*/ 1707 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy) 1708 { 1709 PetscErrorCode ierr; 1710 PetscViewer viewer = (PetscViewer) dummy; 1711 1712 PetscFunctionBegin; 1713 if (!viewer) { 1714 ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); 1715 } 1716 ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr); 1717 PetscFunctionReturn(0); 1718 } 1719 1720 #undef __FUNCT__ 1721 #define __FUNCT__ "PetscOptionsMonitorSet" 1722 /*@C 1723 PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that 1724 modified the PETSc options database. 1725 1726 Not collective 1727 1728 Input Parameters: 1729 + monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring 1730 . mctx - [optional] context for private data for the 1731 monitor routine (use PETSC_NULL if no context is desired) 1732 - monitordestroy - [optional] routine that frees monitor context 1733 (may be PETSC_NULL) 1734 1735 Calling Sequence of monitor: 1736 $ monitor (const char name[], const char value[], void *mctx) 1737 1738 + name - option name string 1739 . value - option value string 1740 - mctx - optional monitoring context, as set by PetscOptionsMonitorSet() 1741 1742 Options Database Keys: 1743 + -options_monitor - sets PetscOptionsMonitorDefault() 1744 - -options_monitor_cancel - cancels all monitors that have 1745 been hardwired into a code by 1746 calls to PetscOptionsMonitorSet(), but 1747 does not cancel those set via 1748 the options database. 1749 1750 Notes: 1751 The default is to do nothing. To print the name and value of options 1752 being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine, 1753 with a null monitoring context. 1754 1755 Several different monitoring routines may be set by calling 1756 PetscOptionsMonitorSet() multiple times; all will be called in the 1757 order in which they were set. 1758 1759 Level: beginner 1760 1761 .keywords: PetscOptions, set, monitor 1762 1763 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel() 1764 @*/ 1765 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 1766 { 1767 PetscFunctionBegin; 1768 if (options->numbermonitors >= MAXOPTIONSMONITORS) { 1769 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set"); 1770 } 1771 options->monitor[options->numbermonitors] = monitor; 1772 options->monitordestroy[options->numbermonitors] = monitordestroy; 1773 options->monitorcontext[options->numbermonitors++] = (void*)mctx; 1774 PetscFunctionReturn(0); 1775 } 1776 1777 #undef __FUNCT__ 1778 #define __FUNCT__ "PetscOptionsMonitorCancel" 1779 /*@ 1780 PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object. 1781 1782 Not collective 1783 1784 Options Database Key: 1785 . -options_monitor_cancel - Cancels all monitors that have 1786 been hardwired into a code by calls to PetscOptionsMonitorSet(), 1787 but does not cancel those set via the options database. 1788 1789 Level: intermediate 1790 1791 .keywords: PetscOptions, set, monitor 1792 1793 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet() 1794 @*/ 1795 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorCancel(void) 1796 { 1797 PetscErrorCode ierr; 1798 PetscInt i; 1799 1800 PetscFunctionBegin; 1801 for (i=0; i<options->numbermonitors; i++) { 1802 if (options->monitordestroy[i]) { 1803 ierr = (*options->monitordestroy[i])(options->monitorcontext[i]);CHKERRQ(ierr); 1804 } 1805 } 1806 options->numbermonitors = 0; 1807 PetscFunctionReturn(0); 1808 } 1809