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