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