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