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