1 /* Define Feature test macros to make sure atoll is available (SVr4, POSIX.1-2001, 4.3BSD, C99), not in (C89 and POSIX.1-1996) */ 2 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for atoll() */ 3 4 /* 5 These routines simplify the use of command line, file options, etc., and are used to manipulate the options database. 6 This provides the low-level interface, the high level interface is in aoptions.c 7 8 Some routines use regular malloc and free because it cannot know what malloc is requested with the 9 options database until it has already processed the input. 10 */ 11 12 #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 13 #include <petscviewer.h> 14 #include <ctype.h> 15 #if defined(PETSC_HAVE_MALLOC_H) 16 #include <malloc.h> 17 #endif 18 #if defined(PETSC_HAVE_STRINGS_H) 19 # include <strings.h> /* strcasecmp */ 20 #endif 21 22 #if defined(PETSC_HAVE_STRCASECMP) 23 #define PetscOptNameCmp(a,b) strcasecmp(a,b) 24 #elif defined(PETSC_HAVE_STRICMP) 25 #define PetscOptNameCmp(a,b) stricmp(a,b) 26 #else 27 #define PetscOptNameCmp(a,b) Error_strcasecmp_not_found 28 #endif 29 30 #include <petsc/private/hashtable.h> 31 32 /* This assumes ASCII encoding and ignores locale settings */ 33 /* Using tolower() is about 2X slower in microbenchmarks */ 34 PETSC_STATIC_INLINE int PetscToLower(int c) 35 { 36 return ((c >= 'A') & (c <= 'Z')) ? c + 'a' - 'A' : c; 37 } 38 39 /* Bob Jenkins's one at a time hash function (case-insensitive) */ 40 PETSC_STATIC_INLINE unsigned int PetscOptHash(const char key[]) 41 { 42 unsigned int hash = 0; 43 while (*key) { 44 hash += PetscToLower(*key++); 45 hash += hash << 10; 46 hash ^= hash >> 6; 47 } 48 hash += hash << 3; 49 hash ^= hash >> 11; 50 hash += hash << 15; 51 return hash; 52 } 53 54 PETSC_STATIC_INLINE int PetscOptEqual(const char a[],const char b[]) 55 { 56 return !PetscOptNameCmp(a,b); 57 } 58 59 KHASH_INIT(HO, kh_cstr_t, int, 1, PetscOptHash, PetscOptEqual) 60 61 /* 62 This table holds all the options set by the user. For simplicity, we use a static size database 63 */ 64 #define MAXOPTNAME PETSC_MAX_OPTION_NAME 65 #define MAXOPTIONS 512 66 #define MAXALIASES 25 67 #define MAXPREFIXES 25 68 #define MAXOPTIONSMONITORS 5 69 70 struct _n_PetscOptions { 71 PetscOptions previous; 72 int N; /* number of options */ 73 char *names[MAXOPTIONS]; /* option names */ 74 char *values[MAXOPTIONS]; /* option values */ 75 PetscBool used[MAXOPTIONS]; /* flag option use */ 76 PetscBool precedentProcessed; 77 78 /* Hash table */ 79 khash_t(HO) *ht; 80 81 /* Prefixes */ 82 int prefixind; 83 int prefixstack[MAXPREFIXES]; 84 char prefix[MAXOPTNAME]; 85 86 /* Aliases */ 87 int Naliases; /* number or aliases */ 88 char *aliases1[MAXALIASES]; /* aliased */ 89 char *aliases2[MAXALIASES]; /* aliasee */ 90 91 /* Help */ 92 PetscBool help; /* flag whether "-help" is in the database */ 93 PetscBool help_intro; /* flag whether "-help intro" is in the database */ 94 95 /* Monitors */ 96 PetscBool monitorFromOptions, monitorCancel; 97 PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[],const char[],void*); /* returns control to user after */ 98 PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void**); /* */ 99 void *monitorcontext[MAXOPTIONSMONITORS]; /* to pass arbitrary user data into monitor */ 100 PetscInt numbermonitors; /* to, for instance, detect options being set */ 101 }; 102 103 static PetscOptions defaultoptions = NULL; /* the options database routines query this object for options */ 104 105 /* list of options which preceed others, i.e., are processed in PetscOptionsProcessPrecedentFlags() */ 106 static const char *precedentOptions[] = {"-options_monitor","-options_monitor_cancel","-help","-skip_petscrc"}; 107 enum PetscPrecedentOption {PO_OPTIONS_MONITOR,PO_OPTIONS_MONITOR_CANCEL,PO_HELP,PO_SKIP_PETSCRC,PO_NUM}; 108 109 static PetscErrorCode PetscOptionsSetValue_Private(PetscOptions,const char[],const char[],int*); 110 111 /* 112 Options events monitor 113 */ 114 static PetscErrorCode PetscOptionsMonitor(PetscOptions options,const char name[],const char value[]) 115 { 116 PetscInt i; 117 PetscErrorCode ierr; 118 119 if (!PetscErrorHandlingInitialized) return 0; 120 PetscFunctionBegin; 121 if (!value) value = ""; 122 if (options->monitorFromOptions) { 123 ierr = PetscOptionsMonitorDefault(name,value,NULL);CHKERRQ(ierr); 124 } 125 for (i=0; i<options->numbermonitors; i++) { 126 ierr = (*options->monitor[i])(name,value,options->monitorcontext[i]);CHKERRQ(ierr); 127 } 128 PetscFunctionReturn(0); 129 } 130 131 /*@ 132 PetscOptionsCreate - Creates an empty options database. 133 134 Logically collective 135 136 Output Parameter: 137 . options - Options database object 138 139 Level: advanced 140 141 Developer Note: We may want eventually to pass a MPI_Comm to determine the ownership of the object 142 143 .seealso: PetscOptionsDestroy(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsInsert(), PetscOptionsSetValue() 144 @*/ 145 PetscErrorCode PetscOptionsCreate(PetscOptions *options) 146 { 147 if (!options) return PETSC_ERR_ARG_NULL; 148 *options = (PetscOptions)calloc(1,sizeof(**options)); 149 if (!*options) return PETSC_ERR_MEM; 150 return 0; 151 } 152 153 /*@ 154 PetscOptionsDestroy - Destroys an option database. 155 156 Logically collective on whatever communicator was associated with the call to PetscOptionsCreate() 157 158 Input Parameter: 159 . options - the PetscOptions object 160 161 Level: advanced 162 163 .seealso: PetscOptionsInsert(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsInsert(), PetscOptionsSetValue() 164 @*/ 165 PetscErrorCode PetscOptionsDestroy(PetscOptions *options) 166 { 167 PetscErrorCode ierr; 168 169 if (!*options) return 0; 170 if ((*options)->previous) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"You are destroying an option that has been used with PetscOptionsPush() but does not have a corresponding PetscOptionsPop()"); 171 ierr = PetscOptionsClear(*options);if (ierr) return ierr; 172 /* XXX what about monitors ? */ 173 free(*options); 174 *options = NULL; 175 PetscFunctionReturn(0); 176 } 177 178 /* 179 PetscOptionsCreateDefault - Creates the default global options database 180 */ 181 PetscErrorCode PetscOptionsCreateDefault(void) 182 { 183 PetscErrorCode ierr; 184 185 if (!defaultoptions) { 186 ierr = PetscOptionsCreate(&defaultoptions);if (ierr) return ierr; 187 } 188 return 0; 189 } 190 191 /*@ 192 PetscOptionsPush - Push a new PetscOptions object as the default provider of options 193 Allows using different parts of a code to use different options databases 194 195 Logically Collective 196 197 Input Parameter: 198 . opt - the options obtained with PetscOptionsCreate() 199 200 Notes: 201 Use PetscOptionsPop() to return to the previous default options database 202 203 The collectivity of this routine is complex; only the MPI processes that call this routine will 204 have the affect of these options. If some processes that create objects call this routine and others do 205 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 206 on different ranks. 207 208 Level: advanced 209 210 .seealso: PetscOptionsPop(), PetscOptionsCreate(), PetscOptionsInsert(), PetscOptionsSetValue(), PetscOptionsLeft() 211 212 @*/ 213 PetscErrorCode PetscOptionsPush(PetscOptions opt) 214 { 215 PetscErrorCode ierr; 216 217 PetscFunctionBegin; 218 ierr = PetscOptionsCreateDefault();CHKERRQ(ierr); 219 opt->previous = defaultoptions; 220 defaultoptions = opt; 221 PetscFunctionReturn(0); 222 } 223 224 /*@ 225 PetscOptionsPop - Pop the most recent PetscOptionsPush() to return to the previous default options 226 227 Logically collective on whatever communicator was associated with the call to PetscOptionsCreate() 228 229 Notes: 230 Use PetscOptionsPop() to return to the previous default options database 231 Allows using different parts of a code to use different options databases 232 233 Level: advanced 234 235 .seealso: PetscOptionsPop(), PetscOptionsCreate(), PetscOptionsInsert(), PetscOptionsSetValue(), PetscOptionsLeft() 236 237 @*/ 238 PetscErrorCode PetscOptionsPop(void) 239 { 240 PetscOptions current = defaultoptions; 241 242 PetscFunctionBegin; 243 if (!defaultoptions) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing default options"); 244 if (!defaultoptions->previous) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscOptionsPop() called too many times"); 245 defaultoptions = defaultoptions->previous; 246 current->previous = NULL; 247 PetscFunctionReturn(0); 248 } 249 250 /* 251 PetscOptionsDestroyDefault - Destroys the default global options database 252 */ 253 PetscErrorCode PetscOptionsDestroyDefault(void) 254 { 255 PetscErrorCode ierr; 256 PetscOptions tmp; 257 258 if (!defaultoptions) return 0; 259 /* Destroy any options that the user forgot to pop */ 260 while (defaultoptions->previous) { 261 tmp = defaultoptions; 262 ierr = PetscOptionsPop();CHKERRQ(ierr); 263 ierr = PetscOptionsDestroy(&tmp);CHKERRQ(ierr); 264 } 265 ierr = PetscOptionsDestroy(&defaultoptions);if (ierr) return ierr; 266 return 0; 267 } 268 269 /*@C 270 PetscOptionsValidKey - PETSc Options database keys must begin with one or two dashes (-) followed by a letter. 271 272 Not collective 273 274 Input Parameter: 275 . key - string to check if valid 276 277 Output Parameter: 278 . valid - PETSC_TRUE if a valid key 279 280 Level: intermediate 281 @*/ 282 PetscErrorCode PetscOptionsValidKey(const char key[],PetscBool *valid) 283 { 284 char *ptr; 285 286 PetscFunctionBegin; 287 if (key) PetscValidCharPointer(key,1); 288 PetscValidPointer(valid,2); 289 *valid = PETSC_FALSE; 290 if (!key) PetscFunctionReturn(0); 291 if (key[0] != '-') PetscFunctionReturn(0); 292 if (key[1] == '-') key++; 293 if (!isalpha((int)key[1])) PetscFunctionReturn(0); 294 (void) strtod(key,&ptr); 295 if (ptr != key && !(*ptr == '_' || isalnum((int)*ptr))) PetscFunctionReturn(0); 296 *valid = PETSC_TRUE; 297 PetscFunctionReturn(0); 298 } 299 300 /*@C 301 PetscOptionsInsertString - Inserts options into the database from a string 302 303 Logically Collective 304 305 Input Parameter: 306 + options - options object 307 - in_str - string that contains options separated by blanks 308 309 Level: intermediate 310 311 The collectivity of this routine is complex; only the MPI processes that call this routine will 312 have the affect of these options. If some processes that create objects call this routine and others do 313 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 314 on different ranks. 315 316 Contributed by Boyana Norris 317 318 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(), 319 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(), 320 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 321 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 322 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 323 PetscOptionsFList(), PetscOptionsEList(), PetscOptionsInsertFile() 324 @*/ 325 PetscErrorCode PetscOptionsInsertString(PetscOptions options,const char in_str[]) 326 { 327 MPI_Comm comm = PETSC_COMM_SELF; 328 PetscErrorCode ierr; 329 char *first,*second; 330 PetscToken token; 331 332 PetscFunctionBegin; 333 ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr); 334 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 335 while (first) { 336 PetscBool isfile,isfileyaml,isstringyaml,ispush,ispop,key; 337 ierr = PetscStrcasecmp(first,"-options_file",&isfile);CHKERRQ(ierr); 338 ierr = PetscStrcasecmp(first,"-options_file_yaml",&isfileyaml);CHKERRQ(ierr); 339 ierr = PetscStrcasecmp(first,"-options_string_yaml",&isstringyaml);CHKERRQ(ierr); 340 ierr = PetscStrcasecmp(first,"-prefix_push",&ispush);CHKERRQ(ierr); 341 ierr = PetscStrcasecmp(first,"-prefix_pop",&ispop);CHKERRQ(ierr); 342 ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr); 343 if (!key) { 344 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 345 } else if (isfile) { 346 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 347 ierr = PetscOptionsInsertFile(comm,options,second,PETSC_TRUE);CHKERRQ(ierr); 348 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 349 } else if (isfileyaml) { 350 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 351 ierr = PetscOptionsInsertFileYAML(comm,options,second,PETSC_TRUE);CHKERRQ(ierr); 352 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 353 } else if (isstringyaml) { 354 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 355 ierr = PetscOptionsInsertStringYAML(options,second);CHKERRQ(ierr); 356 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 357 } else if (ispush) { 358 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 359 ierr = PetscOptionsPrefixPush(options,second);CHKERRQ(ierr); 360 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 361 } else if (ispop) { 362 ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr); 363 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 364 } else { 365 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 366 ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr); 367 if (!key) { 368 ierr = PetscOptionsSetValue(options,first,second);CHKERRQ(ierr); 369 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 370 } else { 371 ierr = PetscOptionsSetValue(options,first,NULL);CHKERRQ(ierr); 372 first = second; 373 } 374 } 375 } 376 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 377 PetscFunctionReturn(0); 378 } 379 380 /* 381 Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free() 382 */ 383 static char *Petscgetline(FILE * f) 384 { 385 size_t size = 0; 386 size_t len = 0; 387 size_t last = 0; 388 char *buf = NULL; 389 390 if (feof(f)) return NULL; 391 do { 392 size += 1024; /* BUFSIZ is defined as "the optimal read size for this platform" */ 393 buf = (char*)realloc((void*)buf,size); /* realloc(NULL,n) is the same as malloc(n) */ 394 /* Actually do the read. Note that fgets puts a terminal '\0' on the 395 end of the string, so we make sure we overwrite this */ 396 if (!fgets(buf+len,1024,f)) buf[len]=0; 397 PetscStrlen(buf,&len); 398 last = len - 1; 399 } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r'); 400 if (len) return buf; 401 free(buf); 402 return NULL; 403 } 404 405 /*@C 406 PetscOptionsInsertFile - Inserts options into the database from a file. 407 408 Collective 409 410 Input Parameter: 411 + comm - the processes that will share the options (usually PETSC_COMM_WORLD) 412 . options - options database, use NULL for default global database 413 . file - name of file 414 - require - if PETSC_TRUE will generate an error if the file does not exist 415 416 417 Notes: 418 Use # for lines that are comments and which should be ignored. 419 Usually, instead of using this command, one should list the file name in the call to PetscInitialize(), this insures that certain options 420 such as -log_view or -malloc_debug are processed properly. This routine only sets options into the options database that will be processed by later 421 calls to XXXSetFromOptions() it should not be used for options listed under PetscInitialize(). 422 The collectivity of this routine is complex; only the MPI processes in comm will 423 have the affect of these options. If some processes that create objects call this routine and others do 424 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 425 on different ranks. 426 427 Level: developer 428 429 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(), 430 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(), 431 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 432 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 433 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 434 PetscOptionsFList(), PetscOptionsEList() 435 436 @*/ 437 PetscErrorCode PetscOptionsInsertFile(MPI_Comm comm,PetscOptions options,const char file[],PetscBool require) 438 { 439 char *string,*vstring = NULL,*astring = NULL,*packed = NULL; 440 char *tokens[4]; 441 PetscErrorCode ierr; 442 size_t i,len,bytes; 443 FILE *fd; 444 PetscToken token=NULL; 445 int err; 446 char *cmatch; 447 const char cmt='#'; 448 PetscInt line=1; 449 PetscMPIInt rank,cnt=0,acnt=0,counts[2]; 450 PetscBool isdir,alias=PETSC_FALSE,valid; 451 452 PetscFunctionBegin; 453 ierr = PetscMemzero(tokens,sizeof(tokens));CHKERRQ(ierr); 454 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 455 if (!rank) { 456 char fpath[PETSC_MAX_PATH_LEN]; 457 char fname[PETSC_MAX_PATH_LEN]; 458 459 ierr = PetscStrreplace(PETSC_COMM_SELF,file,fpath,sizeof(fpath));CHKERRQ(ierr); 460 ierr = PetscFixFilename(fpath,fname);CHKERRQ(ierr); 461 462 fd = fopen(fname,"r"); 463 ierr = PetscTestDirectory(fname,'r',&isdir);CHKERRQ(ierr); 464 if (isdir && require) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Specified options file %s is a directory",fname); 465 if (fd && !isdir) { 466 PetscSegBuffer vseg,aseg; 467 ierr = PetscSegBufferCreate(1,4000,&vseg);CHKERRQ(ierr); 468 ierr = PetscSegBufferCreate(1,2000,&aseg);CHKERRQ(ierr); 469 470 /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */ 471 ierr = PetscInfo1(NULL,"Opened options file %s\n",file);CHKERRQ(ierr); 472 473 while ((string = Petscgetline(fd))) { 474 /* eliminate comments from each line */ 475 ierr = PetscStrchr(string,cmt,&cmatch);CHKERRQ(ierr); 476 if (cmatch) *cmatch = 0; 477 ierr = PetscStrlen(string,&len);CHKERRQ(ierr); 478 /* replace tabs, ^M, \n with " " */ 479 for (i=0; i<len; i++) { 480 if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') { 481 string[i] = ' '; 482 } 483 } 484 ierr = PetscTokenCreate(string,' ',&token);CHKERRQ(ierr); 485 ierr = PetscTokenFind(token,&tokens[0]);CHKERRQ(ierr); 486 if (!tokens[0]) { 487 goto destroy; 488 } else if (!tokens[0][0]) { /* if token 0 is empty (string begins with spaces), redo */ 489 ierr = PetscTokenFind(token,&tokens[0]);CHKERRQ(ierr); 490 } 491 for (i=1; i<4; i++) { 492 ierr = PetscTokenFind(token,&tokens[i]);CHKERRQ(ierr); 493 } 494 if (!tokens[0]) { 495 goto destroy; 496 } else if (tokens[0][0] == '-') { 497 ierr = PetscOptionsValidKey(tokens[0],&valid);CHKERRQ(ierr); 498 if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: invalid option %s",fname,line,tokens[0]); 499 ierr = PetscStrlen(tokens[0],&len);CHKERRQ(ierr); 500 ierr = PetscSegBufferGet(vseg,len+1,&vstring);CHKERRQ(ierr); 501 ierr = PetscArraycpy(vstring,tokens[0],len);CHKERRQ(ierr); 502 vstring[len] = ' '; 503 if (tokens[1]) { 504 ierr = PetscOptionsValidKey(tokens[1],&valid);CHKERRQ(ierr); 505 if (valid) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: cannot specify two options per line (%s %s)",fname,line,tokens[0],tokens[1]); 506 ierr = PetscStrlen(tokens[1],&len);CHKERRQ(ierr); 507 ierr = PetscSegBufferGet(vseg,len+3,&vstring);CHKERRQ(ierr); 508 vstring[0] = '"'; 509 ierr = PetscArraycpy(vstring+1,tokens[1],len);CHKERRQ(ierr); 510 vstring[len+1] = '"'; 511 vstring[len+2] = ' '; 512 } 513 } else { 514 ierr = PetscStrcasecmp(tokens[0],"alias",&alias);CHKERRQ(ierr); 515 if (alias) { 516 ierr = PetscOptionsValidKey(tokens[1],&valid);CHKERRQ(ierr); 517 if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: invalid aliased option %s",fname,line,tokens[1]); 518 if (!tokens[2]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: alias missing for %s",fname,line,tokens[1]); 519 ierr = PetscOptionsValidKey(tokens[2],&valid);CHKERRQ(ierr); 520 if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: invalid aliasee option %s",fname,line,tokens[2]); 521 ierr = PetscStrlen(tokens[1],&len);CHKERRQ(ierr); 522 ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr); 523 ierr = PetscArraycpy(astring,tokens[1],len);CHKERRQ(ierr); 524 astring[len] = ' '; 525 526 ierr = PetscStrlen(tokens[2],&len);CHKERRQ(ierr); 527 ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr); 528 ierr = PetscArraycpy(astring,tokens[2],len);CHKERRQ(ierr); 529 astring[len] = ' '; 530 } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown first token in options file %s line %D: %s",fname,line,tokens[0]); 531 } 532 { 533 const char *extraToken = alias ? tokens[3] : tokens[2]; 534 if (extraToken) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file %s line %D: extra token %s",fname,line,extraToken); 535 } 536 destroy: 537 free(string); 538 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 539 alias = PETSC_FALSE; 540 line++; 541 } 542 err = fclose(fd); 543 if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file %s",fname); 544 ierr = PetscSegBufferGetSize(aseg,&bytes);CHKERRQ(ierr); /* size without null termination */ 545 ierr = PetscMPIIntCast(bytes,&acnt);CHKERRQ(ierr); 546 ierr = PetscSegBufferGet(aseg,1,&astring);CHKERRQ(ierr); 547 astring[0] = 0; 548 ierr = PetscSegBufferGetSize(vseg,&bytes);CHKERRQ(ierr); /* size without null termination */ 549 ierr = PetscMPIIntCast(bytes,&cnt);CHKERRQ(ierr); 550 ierr = PetscSegBufferGet(vseg,1,&vstring);CHKERRQ(ierr); 551 vstring[0] = 0; 552 ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr); 553 ierr = PetscSegBufferExtractTo(aseg,packed);CHKERRQ(ierr); 554 ierr = PetscSegBufferExtractTo(vseg,packed+acnt+1);CHKERRQ(ierr); 555 ierr = PetscSegBufferDestroy(&aseg);CHKERRQ(ierr); 556 ierr = PetscSegBufferDestroy(&vseg);CHKERRQ(ierr); 557 } else if (require) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Unable to open options file %s",fname); 558 } 559 560 counts[0] = acnt; 561 counts[1] = cnt; 562 err = MPI_Bcast(counts,2,MPI_INT,0,comm); 563 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in first MPI collective call, could be caused by using an incorrect mpiexec or a network problem, it can be caused by having VPN running: see https://www.mcs.anl.gov/petsc/documentation/faq.html"); 564 acnt = counts[0]; 565 cnt = counts[1]; 566 if (rank) { 567 ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr); 568 } 569 if (acnt || cnt) { 570 ierr = MPI_Bcast(packed,2+acnt+cnt,MPI_CHAR,0,comm);CHKERRMPI(ierr); 571 astring = packed; 572 vstring = packed + acnt + 1; 573 } 574 575 if (acnt) { 576 ierr = PetscTokenCreate(astring,' ',&token);CHKERRQ(ierr); 577 ierr = PetscTokenFind(token,&tokens[0]);CHKERRQ(ierr); 578 while (tokens[0]) { 579 ierr = PetscTokenFind(token,&tokens[1]);CHKERRQ(ierr); 580 ierr = PetscOptionsSetAlias(options,tokens[0],tokens[1]);CHKERRQ(ierr); 581 ierr = PetscTokenFind(token,&tokens[0]);CHKERRQ(ierr); 582 } 583 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 584 } 585 586 if (cnt) { 587 ierr = PetscOptionsInsertString(options,vstring);CHKERRQ(ierr); 588 } 589 ierr = PetscFree(packed);CHKERRQ(ierr); 590 PetscFunctionReturn(0); 591 } 592 593 /*@C 594 PetscOptionsInsertArgs - Inserts options into the database from a array of strings 595 596 Logically Collective 597 598 Input Parameter: 599 + options - options object 600 . argc - the array lenght 601 - args - the string array 602 603 Level: intermediate 604 605 .seealso: PetscOptions, PetscOptionsInsertString(), PetscOptionsInsertFile() 606 @*/ 607 PetscErrorCode PetscOptionsInsertArgs(PetscOptions options,int argc,char *args[]) 608 { 609 MPI_Comm comm = PETSC_COMM_WORLD; 610 PetscErrorCode ierr; 611 int left = PetscMax(argc,0); 612 char *const *eargs = args; 613 614 PetscFunctionBegin; 615 while (left) { 616 PetscBool isfile,isfileyaml,isstringyaml,ispush,ispop,key; 617 ierr = PetscStrcasecmp(eargs[0],"-options_file",&isfile);CHKERRQ(ierr); 618 ierr = PetscStrcasecmp(eargs[0],"-options_file_yaml",&isfileyaml);CHKERRQ(ierr); 619 ierr = PetscStrcasecmp(eargs[0],"-options_string_yaml",&isstringyaml);CHKERRQ(ierr); 620 ierr = PetscStrcasecmp(eargs[0],"-prefix_push",&ispush);CHKERRQ(ierr); 621 ierr = PetscStrcasecmp(eargs[0],"-prefix_pop",&ispop);CHKERRQ(ierr); 622 ierr = PetscOptionsValidKey(eargs[0],&key);CHKERRQ(ierr); 623 if (!key) { 624 eargs++; left--; 625 } else if (isfile) { 626 if (left <= 1 || eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option"); 627 ierr = PetscOptionsInsertFile(comm,options,eargs[1],PETSC_TRUE);CHKERRQ(ierr); 628 eargs += 2; left -= 2; 629 } else if (isfileyaml) { 630 if (left <= 1 || eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file_yaml filename option"); 631 ierr = PetscOptionsInsertFileYAML(comm,options,eargs[1],PETSC_TRUE);CHKERRQ(ierr); 632 eargs += 2; left -= 2; 633 } else if (isstringyaml) { 634 if (left <= 1 || eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing string for -options_string_yaml string option"); 635 ierr = PetscOptionsInsertStringYAML(options,eargs[1]);CHKERRQ(ierr); 636 eargs += 2; left -= 2; 637 } else if (ispush) { 638 if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option"); 639 if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option (prefixes cannot start with '-')"); 640 ierr = PetscOptionsPrefixPush(options,eargs[1]);CHKERRQ(ierr); 641 eargs += 2; left -= 2; 642 } else if (ispop) { 643 ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr); 644 eargs++; left--; 645 } else { 646 PetscBool nextiskey = PETSC_FALSE; 647 if (left >= 2) {ierr = PetscOptionsValidKey(eargs[1],&nextiskey);CHKERRQ(ierr);} 648 if (left < 2 || nextiskey) { 649 ierr = PetscOptionsSetValue(options,eargs[0],NULL);CHKERRQ(ierr); 650 eargs++; left--; 651 } else { 652 ierr = PetscOptionsSetValue(options,eargs[0],eargs[1]);CHKERRQ(ierr); 653 eargs += 2; left -= 2; 654 } 655 } 656 } 657 PetscFunctionReturn(0); 658 } 659 660 PETSC_STATIC_INLINE PetscErrorCode PetscOptionsStringToBoolIfSet_Private(enum PetscPrecedentOption opt,const char *val[],PetscBool set[],PetscBool *flg) 661 { 662 PetscErrorCode ierr; 663 664 PetscFunctionBegin; 665 if (set[opt]) { 666 ierr = PetscOptionsStringToBool(val[opt],flg);CHKERRQ(ierr); 667 } else *flg = PETSC_FALSE; 668 PetscFunctionReturn(0); 669 } 670 671 /* Process options with absolute precedence */ 672 static PetscErrorCode PetscOptionsProcessPrecedentFlags(PetscOptions options,int argc,char *args[],PetscBool *skip_petscrc,PetscBool *skip_petscrc_set) 673 { 674 const char* const *opt = precedentOptions; 675 const size_t n = PO_NUM; 676 size_t o; 677 int a; 678 const char **val; 679 PetscBool *set; 680 PetscErrorCode ierr; 681 682 PetscFunctionBegin; 683 ierr = PetscCalloc2(n,&val,n,&set);CHKERRQ(ierr); 684 685 /* Look for options possibly set using PetscOptionsSetValue beforehand */ 686 for (o=0; o<n; o++) { 687 ierr = PetscOptionsFindPair(options,NULL,opt[o],&val[o],&set[o]);CHKERRQ(ierr); 688 } 689 690 /* Loop through all args to collect last occuring value of each option */ 691 for (a=1; a<argc; a++) { 692 PetscBool valid, eq; 693 694 ierr = PetscOptionsValidKey(args[a],&valid);CHKERRQ(ierr); 695 if (!valid) continue; 696 for (o=0; o<n; o++) { 697 ierr = PetscStrcasecmp(args[a],opt[o],&eq);CHKERRQ(ierr); 698 if (eq) { 699 set[o] = PETSC_TRUE; 700 if (a == argc-1 || !args[a+1] || !args[a+1][0] || args[a+1][0] == '-') val[o] = NULL; 701 else val[o] = args[a+1]; 702 break; 703 } 704 } 705 } 706 707 /* Process flags */ 708 ierr = PetscStrcasecmp(val[PO_HELP], "intro", &options->help_intro);CHKERRQ(ierr); 709 if (options->help_intro) options->help = PETSC_TRUE; 710 else {ierr = PetscOptionsStringToBoolIfSet_Private(PO_HELP, val,set,&options->help);CHKERRQ(ierr);} 711 ierr = PetscOptionsStringToBoolIfSet_Private(PO_OPTIONS_MONITOR_CANCEL,val,set,&options->monitorCancel);CHKERRQ(ierr); 712 ierr = PetscOptionsStringToBoolIfSet_Private(PO_OPTIONS_MONITOR, val,set,&options->monitorFromOptions);CHKERRQ(ierr); 713 ierr = PetscOptionsStringToBoolIfSet_Private(PO_SKIP_PETSCRC, val,set,skip_petscrc);CHKERRQ(ierr); 714 *skip_petscrc_set = set[PO_SKIP_PETSCRC]; 715 716 /* Store precedent options in database and mark them as used */ 717 for (o=0; o<n; o++) { 718 if (set[o]) { 719 ierr = PetscOptionsSetValue_Private(options,opt[o],val[o],&a);CHKERRQ(ierr); 720 options->used[a] = PETSC_TRUE; 721 } 722 } 723 724 ierr = PetscFree2(val,set);CHKERRQ(ierr); 725 options->precedentProcessed = PETSC_TRUE; 726 PetscFunctionReturn(0); 727 } 728 729 PETSC_STATIC_INLINE PetscErrorCode PetscOptionsSkipPrecedent(PetscOptions options,const char name[],PetscBool *flg) 730 { 731 int i; 732 PetscErrorCode ierr; 733 734 *flg = PETSC_FALSE; 735 if (options->precedentProcessed) { 736 for (i=0; i<PO_NUM; i++) { 737 if (!PetscOptNameCmp(precedentOptions[i],name)) { 738 /* check if precedent option has been set already */ 739 ierr = PetscOptionsFindPair(options,NULL,name,NULL,flg);if (ierr) return ierr; 740 if (*flg) break; 741 } 742 } 743 } 744 return 0; 745 } 746 747 /*@C 748 PetscOptionsInsert - Inserts into the options database from the command line, 749 the environmental variable and a file. 750 751 Collective on PETSC_COMM_WORLD 752 753 Input Parameters: 754 + options - options database or NULL for the default global database 755 . argc - count of number of command line arguments 756 . args - the command line arguments 757 - file - [optional] PETSc database file, also checks ~/.petscrc, .petscrc and petscrc. 758 Use NULL to not check for code specific file. 759 Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files. 760 761 Note: 762 Since PetscOptionsInsert() is automatically called by PetscInitialize(), 763 the user does not typically need to call this routine. PetscOptionsInsert() 764 can be called several times, adding additional entries into the database. 765 766 Options Database Keys: 767 + -options_file <filename> - read options from a file 768 - -options_file_yaml <filename> - read options from a YAML file 769 770 See PetscInitialize() for options related to option database monitoring. 771 772 Level: advanced 773 774 .seealso: PetscOptionsDestroy(), PetscOptionsView(), PetscOptionsInsertString(), PetscOptionsInsertFile(), 775 PetscInitialize() 776 @*/ 777 PetscErrorCode PetscOptionsInsert(PetscOptions options,int *argc,char ***args,const char file[]) 778 { 779 MPI_Comm comm = PETSC_COMM_WORLD; 780 PetscErrorCode ierr; 781 PetscMPIInt rank; 782 char filename[PETSC_MAX_PATH_LEN]; 783 PetscBool hasArgs = (argc && *argc) ? PETSC_TRUE : PETSC_FALSE; 784 PetscBool skipPetscrc = PETSC_FALSE, skipPetscrcSet = PETSC_FALSE; 785 786 PetscFunctionBegin; 787 if (hasArgs && !(args && *args)) SETERRQ(comm,PETSC_ERR_ARG_NULL,"*argc > 1 but *args not given"); 788 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 789 790 if (!options) { 791 ierr = PetscOptionsCreateDefault();CHKERRQ(ierr); 792 options = defaultoptions; 793 } 794 if (hasArgs) { 795 /* process options with absolute precedence */ 796 ierr = PetscOptionsProcessPrecedentFlags(options,*argc,*args,&skipPetscrc,&skipPetscrcSet);CHKERRQ(ierr); 797 } 798 if (file && file[0]) { 799 ierr = PetscStrreplace(comm,file,filename,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 800 ierr = PetscOptionsInsertFile(comm,options,filename,PETSC_TRUE);CHKERRQ(ierr); 801 /* if -skip_petscrc has not been set from command line, check whether it has been set in the file */ 802 if (!skipPetscrcSet) {ierr = PetscOptionsGetBool(options,NULL,"-skip_petscrc",&skipPetscrc,NULL);CHKERRQ(ierr);} 803 } 804 if (!skipPetscrc) { 805 ierr = PetscGetHomeDirectory(filename,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr); 806 /* PetscOptionsInsertFile() does a fopen() on rank0 only - so only rank0 HomeDir value is relavent */ 807 if (filename[0]) { ierr = PetscStrcat(filename,"/.petscrc");CHKERRQ(ierr); } 808 ierr = PetscOptionsInsertFile(comm,options,filename,PETSC_FALSE);CHKERRQ(ierr); 809 ierr = PetscOptionsInsertFile(comm,options,".petscrc",PETSC_FALSE);CHKERRQ(ierr); 810 ierr = PetscOptionsInsertFile(comm,options,"petscrc",PETSC_FALSE);CHKERRQ(ierr); 811 } 812 813 /* insert environment options */ 814 { 815 char *eoptions = NULL; 816 size_t len = 0; 817 if (!rank) { 818 eoptions = (char*)getenv("PETSC_OPTIONS"); 819 ierr = PetscStrlen(eoptions,&len);CHKERRQ(ierr); 820 } 821 ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,comm);CHKERRQ(ierr); 822 if (len) { 823 if (rank) {ierr = PetscMalloc1(len+1,&eoptions);CHKERRQ(ierr);} 824 ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,comm);CHKERRMPI(ierr); 825 if (rank) eoptions[len] = 0; 826 ierr = PetscOptionsInsertString(options,eoptions);CHKERRQ(ierr); 827 if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);} 828 } 829 } 830 831 /* insert YAML environment options */ 832 { 833 char *eoptions = NULL; 834 size_t len = 0; 835 if (!rank) { 836 eoptions = (char*)getenv("PETSC_OPTIONS_YAML"); 837 ierr = PetscStrlen(eoptions,&len);CHKERRQ(ierr); 838 } 839 ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,comm);CHKERRQ(ierr); 840 if (len) { 841 if (rank) {ierr = PetscMalloc1(len+1,&eoptions);CHKERRQ(ierr);} 842 ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,comm);CHKERRMPI(ierr); 843 if (rank) eoptions[len] = 0; 844 ierr = PetscOptionsInsertStringYAML(options,eoptions);CHKERRQ(ierr); 845 if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);} 846 } 847 } 848 849 /* insert command line options here because they take precedence over arguments in petscrc/environment */ 850 if (hasArgs) {ierr = PetscOptionsInsertArgs(options,*argc-1,*args+1);CHKERRQ(ierr);} 851 PetscFunctionReturn(0); 852 } 853 854 /*@C 855 PetscOptionsView - Prints the options that have been loaded. This is 856 useful for debugging purposes. 857 858 Logically Collective on PetscViewer 859 860 Input Parameter: 861 + options - options database, use NULL for default global database 862 - viewer - must be an PETSCVIEWERASCII viewer 863 864 Options Database Key: 865 . -options_view - Activates PetscOptionsView() within PetscFinalize() 866 867 Notes: 868 Only the rank zero process of MPI_Comm used to create view prints the option values. Other processes 869 may have different values but they are not printed. 870 871 Level: advanced 872 873 .seealso: PetscOptionsAllUsed() 874 @*/ 875 PetscErrorCode PetscOptionsView(PetscOptions options,PetscViewer viewer) 876 { 877 PetscErrorCode ierr; 878 PetscInt i; 879 PetscBool isascii; 880 881 PetscFunctionBegin; 882 if (viewer) PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 883 options = options ? options : defaultoptions; 884 if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD; 885 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); 886 if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only supports ASCII viewer"); 887 888 if (!options->N) { 889 ierr = PetscViewerASCIIPrintf(viewer,"#No PETSc Option Table entries\n");CHKERRQ(ierr); 890 PetscFunctionReturn(0); 891 } 892 893 ierr = PetscViewerASCIIPrintf(viewer,"#PETSc Option Table entries:\n");CHKERRQ(ierr); 894 for (i=0; i<options->N; i++) { 895 if (options->values[i]) { 896 ierr = PetscViewerASCIIPrintf(viewer,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr); 897 } else { 898 ierr = PetscViewerASCIIPrintf(viewer,"-%s\n",options->names[i]);CHKERRQ(ierr); 899 } 900 } 901 ierr = PetscViewerASCIIPrintf(viewer,"#End of PETSc Option Table entries\n");CHKERRQ(ierr); 902 PetscFunctionReturn(0); 903 } 904 905 /* 906 Called by error handlers to print options used in run 907 */ 908 PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void) 909 { 910 PetscInt i; 911 PetscOptions options = defaultoptions; 912 913 PetscFunctionBegin; 914 if (options->N) { 915 (*PetscErrorPrintf)("PETSc Option Table entries:\n"); 916 } else { 917 (*PetscErrorPrintf)("No PETSc Option Table entries\n"); 918 } 919 for (i=0; i<options->N; i++) { 920 if (options->values[i]) { 921 (*PetscErrorPrintf)("-%s %s\n",options->names[i],options->values[i]); 922 } else { 923 (*PetscErrorPrintf)("-%s\n",options->names[i]); 924 } 925 } 926 PetscFunctionReturn(0); 927 } 928 929 /*@C 930 PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow. 931 932 Logically Collective 933 934 Input Parameter: 935 + options - options database, or NULL for the default global database 936 - prefix - The string to append to the existing prefix 937 938 Options Database Keys: 939 + -prefix_push <some_prefix_> - push the given prefix 940 - -prefix_pop - pop the last prefix 941 942 Notes: 943 It is common to use this in conjunction with -options_file as in 944 945 $ -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop 946 947 where the files no longer require all options to be prefixed with -system2_. 948 949 The collectivity of this routine is complex; only the MPI processes that call this routine will 950 have the affect of these options. If some processes that create objects call this routine and others do 951 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 952 on different ranks. 953 954 Level: advanced 955 956 .seealso: PetscOptionsPrefixPop(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsCreate(), PetscOptionsSetValue() 957 @*/ 958 PetscErrorCode PetscOptionsPrefixPush(PetscOptions options,const char prefix[]) 959 { 960 PetscErrorCode ierr; 961 size_t n; 962 PetscInt start; 963 char key[MAXOPTNAME+1]; 964 PetscBool valid; 965 966 PetscFunctionBegin; 967 PetscValidCharPointer(prefix,1); 968 options = options ? options : defaultoptions; 969 if (options->prefixind >= MAXPREFIXES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum depth of prefix stack %d exceeded, recompile \n src/sys/objects/options.c with larger value for MAXPREFIXES",MAXPREFIXES); 970 key[0] = '-'; /* keys must start with '-' */ 971 ierr = PetscStrncpy(key+1,prefix,sizeof(key)-1);CHKERRQ(ierr); 972 ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr); 973 if (!valid && options->prefixind > 0 && isdigit((int)prefix[0])) valid = PETSC_TRUE; /* If the prefix stack is not empty, make numbers a valid prefix */ 974 if (!valid) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Given prefix \"%s\" not valid (the first character must be a letter%s, do not include leading '-')",prefix,options->prefixind?" or digit":""); 975 start = options->prefixind ? options->prefixstack[options->prefixind-1] : 0; 976 ierr = PetscStrlen(prefix,&n);CHKERRQ(ierr); 977 if (n+1 > sizeof(options->prefix)-start) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum prefix length %d exceeded",sizeof(options->prefix)); 978 ierr = PetscArraycpy(options->prefix+start,prefix,n+1);CHKERRQ(ierr); 979 options->prefixstack[options->prefixind++] = start+n; 980 PetscFunctionReturn(0); 981 } 982 983 /*@C 984 PetscOptionsPrefixPop - Remove the latest options prefix, see PetscOptionsPrefixPush() for details 985 986 Logically Collective on the MPI_Comm that called PetscOptionsPrefixPush() 987 988 Input Parameters: 989 . options - options database, or NULL for the default global database 990 991 Level: advanced 992 993 .seealso: PetscOptionsPrefixPush(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsCreate(), PetscOptionsSetValue() 994 @*/ 995 PetscErrorCode PetscOptionsPrefixPop(PetscOptions options) 996 { 997 PetscInt offset; 998 999 PetscFunctionBegin; 1000 options = options ? options : defaultoptions; 1001 if (options->prefixind < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More prefixes popped than pushed"); 1002 options->prefixind--; 1003 offset = options->prefixind ? options->prefixstack[options->prefixind-1] : 0; 1004 options->prefix[offset] = 0; 1005 PetscFunctionReturn(0); 1006 } 1007 1008 /*@C 1009 PetscOptionsClear - Removes all options form the database leaving it empty. 1010 1011 Logically Collective 1012 1013 Input Parameters: 1014 . options - options database, use NULL for the default global database 1015 1016 The collectivity of this routine is complex; only the MPI processes that call this routine will 1017 have the affect of these options. If some processes that create objects call this routine and others do 1018 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 1019 on different ranks. 1020 1021 Level: developer 1022 1023 .seealso: PetscOptionsInsert() 1024 @*/ 1025 PetscErrorCode PetscOptionsClear(PetscOptions options) 1026 { 1027 PetscInt i; 1028 1029 options = options ? options : defaultoptions; 1030 if (!options) return 0; 1031 1032 for (i=0; i<options->N; i++) { 1033 if (options->names[i]) free(options->names[i]); 1034 if (options->values[i]) free(options->values[i]); 1035 } 1036 options->N = 0; 1037 1038 for (i=0; i<options->Naliases; i++) { 1039 free(options->aliases1[i]); 1040 free(options->aliases2[i]); 1041 } 1042 options->Naliases = 0; 1043 1044 /* destroy hash table */ 1045 kh_destroy(HO,options->ht); 1046 options->ht = NULL; 1047 1048 options->prefixind = 0; 1049 options->prefix[0] = 0; 1050 options->help = PETSC_FALSE; 1051 return 0; 1052 } 1053 1054 /*@C 1055 PetscOptionsSetAlias - Makes a key and alias for another key 1056 1057 Logically Collective 1058 1059 Input Parameters: 1060 + options - options database, or NULL for default global database 1061 . newname - the alias 1062 - oldname - the name that alias will refer to 1063 1064 Level: advanced 1065 1066 The collectivity of this routine is complex; only the MPI processes that call this routine will 1067 have the affect of these options. If some processes that create objects call this routine and others do 1068 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 1069 on different ranks. 1070 1071 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 1072 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(), 1073 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1074 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1075 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 1076 PetscOptionsFList(), PetscOptionsEList() 1077 @*/ 1078 PetscErrorCode PetscOptionsSetAlias(PetscOptions options,const char newname[],const char oldname[]) 1079 { 1080 PetscInt n; 1081 size_t len; 1082 PetscBool valid; 1083 PetscErrorCode ierr; 1084 1085 PetscFunctionBegin; 1086 PetscValidCharPointer(newname,2); 1087 PetscValidCharPointer(oldname,3); 1088 options = options ? options : defaultoptions; 1089 ierr = PetscOptionsValidKey(newname,&valid);CHKERRQ(ierr); 1090 if (!valid) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid aliased option %s",newname); 1091 ierr = PetscOptionsValidKey(oldname,&valid);CHKERRQ(ierr); 1092 if (!valid) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid aliasee option %s",oldname); 1093 1094 n = options->Naliases; 1095 if (n >= MAXALIASES) SETERRQ1(PETSC_COMM_SELF,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); 1096 1097 newname++; oldname++; 1098 ierr = PetscStrlen(newname,&len);CHKERRQ(ierr); 1099 options->aliases1[n] = (char*)malloc((len+1)*sizeof(char)); 1100 ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr); 1101 ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr); 1102 options->aliases2[n] = (char*)malloc((len+1)*sizeof(char)); 1103 ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr); 1104 options->Naliases++; 1105 PetscFunctionReturn(0); 1106 } 1107 1108 /*@C 1109 PetscOptionsSetValue - Sets an option name-value pair in the options 1110 database, overriding whatever is already present. 1111 1112 Logically Collective 1113 1114 Input Parameters: 1115 + options - options database, use NULL for the default global database 1116 . name - name of option, this SHOULD have the - prepended 1117 - value - the option value (not used for all options, so can be NULL) 1118 1119 Level: intermediate 1120 1121 Note: 1122 This function can be called BEFORE PetscInitialize() 1123 1124 The collectivity of this routine is complex; only the MPI processes that call this routine will 1125 have the affect of these options. If some processes that create objects call this routine and others do 1126 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 1127 on different ranks. 1128 1129 Developers Note: Uses malloc() directly because PETSc may not be initialized yet. 1130 1131 .seealso: PetscOptionsInsert(), PetscOptionsClearValue() 1132 @*/ 1133 PetscErrorCode PetscOptionsSetValue(PetscOptions options,const char name[],const char value[]) 1134 { 1135 return PetscOptionsSetValue_Private(options,name,value,NULL); 1136 } 1137 1138 static PetscErrorCode PetscOptionsSetValue_Private(PetscOptions options,const char name[],const char value[],int *pos) 1139 { 1140 size_t len; 1141 int N,n,i; 1142 char **names; 1143 char fullname[MAXOPTNAME] = ""; 1144 PetscBool flg; 1145 PetscErrorCode ierr; 1146 1147 if (!options) { 1148 ierr = PetscOptionsCreateDefault();if (ierr) return ierr; 1149 options = defaultoptions; 1150 } 1151 1152 if (name[0] != '-') return PETSC_ERR_ARG_OUTOFRANGE; 1153 1154 ierr = PetscOptionsSkipPrecedent(options,name,&flg);if (ierr) return ierr; 1155 if (flg) return 0; 1156 1157 name++; /* skip starting dash */ 1158 1159 if (options->prefixind > 0) { 1160 strncpy(fullname,options->prefix,sizeof(fullname)); 1161 fullname[sizeof(fullname)-1] = 0; 1162 strncat(fullname,name,sizeof(fullname)-strlen(fullname)-1); 1163 fullname[sizeof(fullname)-1] = 0; 1164 name = fullname; 1165 } 1166 1167 /* check against aliases */ 1168 N = options->Naliases; 1169 for (i=0; i<N; i++) { 1170 int result = PetscOptNameCmp(options->aliases1[i],name); 1171 if (!result) { name = options->aliases2[i]; break; } 1172 } 1173 1174 /* slow search */ 1175 N = n = options->N; 1176 names = options->names; 1177 for (i=0; i<N; i++) { 1178 int result = PetscOptNameCmp(names[i],name); 1179 if (!result) { 1180 n = i; goto setvalue; 1181 } else if (result > 0) { 1182 n = i; break; 1183 } 1184 } 1185 if (N >= MAXOPTIONS) return PETSC_ERR_MEM; 1186 /* shift remaining values up 1 */ 1187 for (i=N; i>n; i--) { 1188 options->names[i] = options->names[i-1]; 1189 options->values[i] = options->values[i-1]; 1190 options->used[i] = options->used[i-1]; 1191 } 1192 options->names[n] = NULL; 1193 options->values[n] = NULL; 1194 options->used[n] = PETSC_FALSE; 1195 options->N++; 1196 1197 /* destroy hash table */ 1198 kh_destroy(HO,options->ht); 1199 options->ht = NULL; 1200 1201 /* set new name */ 1202 len = strlen(name); 1203 options->names[n] = (char*)malloc((len+1)*sizeof(char)); 1204 if (!options->names[n]) return PETSC_ERR_MEM; 1205 strcpy(options->names[n],name); 1206 1207 setvalue: 1208 /* set new value */ 1209 if (options->values[n]) free(options->values[n]); 1210 len = value ? strlen(value) : 0; 1211 if (len) { 1212 options->values[n] = (char*)malloc((len+1)*sizeof(char)); 1213 if (!options->values[n]) return PETSC_ERR_MEM; 1214 strcpy(options->values[n],value); 1215 } else { 1216 options->values[n] = NULL; 1217 } 1218 1219 /* handle -help so that it can be set from anywhere */ 1220 if (!PetscOptNameCmp(name,"help")) { 1221 options->help = PETSC_TRUE; 1222 options->help_intro = (value && !PetscOptNameCmp(value,"intro")) ? PETSC_TRUE : PETSC_FALSE; 1223 options->used[n] = PETSC_TRUE; 1224 } 1225 1226 if (PetscErrorHandlingInitialized) { 1227 ierr = PetscOptionsMonitor(options,name,value);CHKERRQ(ierr); 1228 } 1229 if (pos) *pos = n; 1230 return 0; 1231 } 1232 1233 /*@C 1234 PetscOptionsClearValue - Clears an option name-value pair in the options 1235 database, overriding whatever is already present. 1236 1237 Logically Collective 1238 1239 Input Parameter: 1240 + options - options database, use NULL for the default global database 1241 - name - name of option, this SHOULD have the - prepended 1242 1243 Level: intermediate 1244 1245 The collectivity of this routine is complex; only the MPI processes that call this routine will 1246 have the affect of these options. If some processes that create objects call this routine and others do 1247 not the code may fail in complicated ways because the same parallel solvers may incorrectly use different options 1248 on different ranks. 1249 1250 .seealso: PetscOptionsInsert() 1251 @*/ 1252 PetscErrorCode PetscOptionsClearValue(PetscOptions options,const char name[]) 1253 { 1254 int N,n,i; 1255 char **names; 1256 PetscErrorCode ierr; 1257 1258 PetscFunctionBegin; 1259 options = options ? options : defaultoptions; 1260 if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name); 1261 if (!PetscOptNameCmp(name,"-help")) options->help = options->help_intro = PETSC_FALSE; 1262 1263 name++; /* skip starting dash */ 1264 1265 /* slow search */ 1266 N = n = options->N; 1267 names = options->names; 1268 for (i=0; i<N; i++) { 1269 int result = PetscOptNameCmp(names[i],name); 1270 if (!result) { 1271 n = i; break; 1272 } else if (result > 0) { 1273 n = N; break; 1274 } 1275 } 1276 if (n == N) PetscFunctionReturn(0); /* it was not present */ 1277 1278 /* remove name and value */ 1279 if (options->names[n]) free(options->names[n]); 1280 if (options->values[n]) free(options->values[n]); 1281 /* shift remaining values down 1 */ 1282 for (i=n; i<N-1; i++) { 1283 options->names[i] = options->names[i+1]; 1284 options->values[i] = options->values[i+1]; 1285 options->used[i] = options->used[i+1]; 1286 } 1287 options->N--; 1288 1289 /* destroy hash table */ 1290 kh_destroy(HO,options->ht); 1291 options->ht = NULL; 1292 1293 ierr = PetscOptionsMonitor(options,name,NULL);CHKERRQ(ierr); 1294 PetscFunctionReturn(0); 1295 } 1296 1297 /*@C 1298 PetscOptionsFindPair - Gets an option name-value pair from the options database. 1299 1300 Not Collective 1301 1302 Input Parameters: 1303 + options - options database, use NULL for the default global database 1304 . pre - the string to prepend to the name or NULL, this SHOULD NOT have the "-" prepended 1305 - name - name of option, this SHOULD have the "-" prepended 1306 1307 Output Parameters: 1308 + value - the option value (optional, not used for all options) 1309 - set - whether the option is set (optional) 1310 1311 Notes: 1312 Each process may find different values or no value depending on how options were inserted into the database 1313 1314 Level: developer 1315 1316 .seealso: PetscOptionsSetValue(), PetscOptionsClearValue() 1317 @*/ 1318 PetscErrorCode PetscOptionsFindPair(PetscOptions options,const char pre[],const char name[],const char *value[],PetscBool *set) 1319 { 1320 char buf[MAXOPTNAME]; 1321 PetscBool usehashtable = PETSC_TRUE; 1322 PetscBool matchnumbers = PETSC_TRUE; 1323 PetscErrorCode ierr; 1324 1325 PetscFunctionBegin; 1326 options = options ? options : defaultoptions; 1327 if (pre && PetscUnlikely(pre[0] == '-')) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix cannot begin with '-': Instead %s",pre); 1328 if (PetscUnlikely(name[0] != '-')) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name); 1329 1330 name++; /* skip starting dash */ 1331 1332 /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */ 1333 if (pre && pre[0]) { 1334 char *ptr = buf; 1335 if (name[0] == '-') { *ptr++ = '-'; name++; } 1336 ierr = PetscStrncpy(ptr,pre,buf+sizeof(buf)-ptr);CHKERRQ(ierr); 1337 ierr = PetscStrlcat(buf,name,sizeof(buf));CHKERRQ(ierr); 1338 name = buf; 1339 } 1340 1341 if (PetscDefined(USE_DEBUG)) { 1342 PetscBool valid; 1343 char key[MAXOPTNAME+1] = "-"; 1344 ierr = PetscStrncpy(key+1,name,sizeof(key)-1);CHKERRQ(ierr); 1345 ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr); 1346 if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name); 1347 } 1348 1349 if (!options->ht && usehashtable) { 1350 int i,ret; 1351 khiter_t it; 1352 khash_t(HO) *ht; 1353 ht = kh_init(HO); 1354 if (PetscUnlikely(!ht)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed"); 1355 ret = kh_resize(HO,ht,options->N*2); /* twice the required size to reduce risk of collisions */ 1356 if (PetscUnlikely(ret)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed"); 1357 for (i=0; i<options->N; i++) { 1358 it = kh_put(HO,ht,options->names[i],&ret); 1359 if (PetscUnlikely(ret != 1)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed"); 1360 kh_val(ht,it) = i; 1361 } 1362 options->ht = ht; 1363 } 1364 1365 if (usehashtable) 1366 { /* fast search */ 1367 khash_t(HO) *ht = options->ht; 1368 khiter_t it = kh_get(HO,ht,name); 1369 if (it != kh_end(ht)) { 1370 int i = kh_val(ht,it); 1371 options->used[i] = PETSC_TRUE; 1372 if (value) *value = options->values[i]; 1373 if (set) *set = PETSC_TRUE; 1374 PetscFunctionReturn(0); 1375 } 1376 } else 1377 { /* slow search */ 1378 int i, N = options->N; 1379 for (i=0; i<N; i++) { 1380 int result = PetscOptNameCmp(options->names[i],name); 1381 if (!result) { 1382 options->used[i] = PETSC_TRUE; 1383 if (value) *value = options->values[i]; 1384 if (set) *set = PETSC_TRUE; 1385 PetscFunctionReturn(0); 1386 } else if (result > 0) { 1387 break; 1388 } 1389 } 1390 } 1391 1392 /* 1393 The following block slows down all lookups in the most frequent path (most lookups are unsuccessful). 1394 Maybe this special lookup mode should be enabled on request with a push/pop API. 1395 The feature of matching _%d_ used sparingly in the codebase. 1396 */ 1397 if (matchnumbers) { 1398 int i,j,cnt = 0,locs[16],loce[16]; 1399 /* determine the location and number of all _%d_ in the key */ 1400 for (i=0; name[i]; i++) { 1401 if (name[i] == '_') { 1402 for (j=i+1; name[j]; j++) { 1403 if (name[j] >= '0' && name[j] <= '9') continue; 1404 if (name[j] == '_' && j > i+1) { /* found a number */ 1405 locs[cnt] = i+1; 1406 loce[cnt++] = j+1; 1407 } 1408 i = j-1; 1409 break; 1410 } 1411 } 1412 } 1413 for (i=0; i<cnt; i++) { 1414 PetscBool found; 1415 char opt[MAXOPTNAME+1] = "-", tmp[MAXOPTNAME]; 1416 ierr = PetscStrncpy(tmp,name,PetscMin((size_t)(locs[i]+1),sizeof(tmp)));CHKERRQ(ierr); 1417 ierr = PetscStrlcat(opt,tmp,sizeof(opt));CHKERRQ(ierr); 1418 ierr = PetscStrlcat(opt,name+loce[i],sizeof(opt));CHKERRQ(ierr); 1419 ierr = PetscOptionsFindPair(options,NULL,opt,value,&found);CHKERRQ(ierr); 1420 if (found) {if (set) *set = PETSC_TRUE; PetscFunctionReturn(0);} 1421 } 1422 } 1423 1424 if (set) *set = PETSC_FALSE; 1425 PetscFunctionReturn(0); 1426 } 1427 1428 /* Check whether any option begins with pre+name */ 1429 PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions options,const char pre[], const char name[],const char *value[],PetscBool *set) 1430 { 1431 char buf[MAXOPTNAME]; 1432 int numCnt = 0, locs[16],loce[16]; 1433 PetscErrorCode ierr; 1434 1435 PetscFunctionBegin; 1436 options = options ? options : defaultoptions; 1437 if (pre && pre[0] == '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix cannot begin with '-': Instead %s",pre); 1438 if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name); 1439 1440 name++; /* skip starting dash */ 1441 1442 /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */ 1443 if (pre && pre[0]) { 1444 char *ptr = buf; 1445 if (name[0] == '-') { *ptr++ = '-'; name++; } 1446 ierr = PetscStrncpy(ptr,pre,sizeof(buf)+(size_t)(ptr-buf));CHKERRQ(ierr); 1447 ierr = PetscStrlcat(buf,name,sizeof(buf));CHKERRQ(ierr); 1448 name = buf; 1449 } 1450 1451 if (PetscDefined(USE_DEBUG)) { 1452 PetscBool valid; 1453 char key[MAXOPTNAME+1] = "-"; 1454 ierr = PetscStrncpy(key+1,name,sizeof(key)-1);CHKERRQ(ierr); 1455 ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr); 1456 if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name); 1457 } 1458 1459 /* determine the location and number of all _%d_ in the key */ 1460 { 1461 int i,j; 1462 for (i=0; name[i]; i++) { 1463 if (name[i] == '_') { 1464 for (j=i+1; name[j]; j++) { 1465 if (name[j] >= '0' && name[j] <= '9') continue; 1466 if (name[j] == '_' && j > i+1) { /* found a number */ 1467 locs[numCnt] = i+1; 1468 loce[numCnt++] = j+1; 1469 } 1470 i = j-1; 1471 break; 1472 } 1473 } 1474 } 1475 } 1476 1477 { /* slow search */ 1478 int c, i; 1479 size_t len; 1480 PetscBool match; 1481 1482 for (c = -1; c < numCnt; ++c) { 1483 char opt[MAXOPTNAME+1] = "", tmp[MAXOPTNAME]; 1484 1485 if (c < 0) { 1486 ierr = PetscStrcpy(opt,name);CHKERRQ(ierr); 1487 } else { 1488 ierr = PetscStrncpy(tmp,name,PetscMin((size_t)(locs[c]+1),sizeof(tmp)));CHKERRQ(ierr); 1489 ierr = PetscStrlcat(opt,tmp,sizeof(opt));CHKERRQ(ierr); 1490 ierr = PetscStrlcat(opt,name+loce[c],sizeof(opt));CHKERRQ(ierr); 1491 } 1492 ierr = PetscStrlen(opt,&len);CHKERRQ(ierr); 1493 for (i=0; i<options->N; i++) { 1494 ierr = PetscStrncmp(options->names[i],opt,len,&match);CHKERRQ(ierr); 1495 if (match) { 1496 options->used[i] = PETSC_TRUE; 1497 if (value) *value = options->values[i]; 1498 if (set) *set = PETSC_TRUE; 1499 PetscFunctionReturn(0); 1500 } 1501 } 1502 } 1503 } 1504 1505 if (set) *set = PETSC_FALSE; 1506 PetscFunctionReturn(0); 1507 } 1508 1509 /*@C 1510 PetscOptionsReject - Generates an error if a certain option is given. 1511 1512 Not Collective 1513 1514 Input Parameters: 1515 + options - options database, use NULL for default global database 1516 . pre - the option prefix (may be NULL) 1517 . name - the option name one is seeking 1518 - mess - error message (may be NULL) 1519 1520 Level: advanced 1521 1522 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 1523 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 1524 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1525 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1526 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 1527 PetscOptionsFList(), PetscOptionsEList() 1528 @*/ 1529 PetscErrorCode PetscOptionsReject(PetscOptions options,const char pre[],const char name[],const char mess[]) 1530 { 1531 PetscErrorCode ierr; 1532 PetscBool flag = PETSC_FALSE; 1533 1534 PetscFunctionBegin; 1535 ierr = PetscOptionsHasName(options,pre,name,&flag);CHKERRQ(ierr); 1536 if (flag) { 1537 if (mess && mess[0]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: -%s%s with %s",pre?pre:"",name+1,mess); 1538 else SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: -%s%s",pre?pre:"",name+1); 1539 } 1540 PetscFunctionReturn(0); 1541 } 1542 1543 /*@C 1544 PetscOptionsHasHelp - Determines whether the "-help" option is in the database. 1545 1546 Not Collective 1547 1548 Input Parameters: 1549 . options - options database, use NULL for default global database 1550 1551 Output Parameters: 1552 . set - PETSC_TRUE if found else PETSC_FALSE. 1553 1554 Level: advanced 1555 1556 .seealso: PetscOptionsHasName() 1557 @*/ 1558 PetscErrorCode PetscOptionsHasHelp(PetscOptions options,PetscBool *set) 1559 { 1560 PetscFunctionBegin; 1561 PetscValidPointer(set,2); 1562 options = options ? options : defaultoptions; 1563 *set = options->help; 1564 PetscFunctionReturn(0); 1565 } 1566 1567 PetscErrorCode PetscOptionsHasHelpIntro_Internal(PetscOptions options,PetscBool *set) 1568 { 1569 PetscFunctionBegin; 1570 PetscValidPointer(set,2); 1571 options = options ? options : defaultoptions; 1572 *set = options->help_intro; 1573 PetscFunctionReturn(0); 1574 } 1575 1576 /*@C 1577 PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even 1578 its value is set to false. 1579 1580 Not Collective 1581 1582 Input Parameters: 1583 + options - options database, use NULL for default global database 1584 . pre - string to prepend to the name or NULL 1585 - name - the option one is seeking 1586 1587 Output Parameters: 1588 . set - PETSC_TRUE if found else PETSC_FALSE. 1589 1590 Level: beginner 1591 1592 Notes: 1593 In many cases you probably want to use PetscOptionsGetBool() instead of calling this, to allowing toggling values. 1594 1595 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1596 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 1597 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1598 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1599 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 1600 PetscOptionsFList(), PetscOptionsEList() 1601 @*/ 1602 PetscErrorCode PetscOptionsHasName(PetscOptions options,const char pre[],const char name[],PetscBool *set) 1603 { 1604 const char *value; 1605 PetscErrorCode ierr; 1606 PetscBool flag; 1607 1608 PetscFunctionBegin; 1609 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 1610 if (set) *set = flag; 1611 PetscFunctionReturn(0); 1612 } 1613 1614 /*@C 1615 PetscOptionsGetAll - Lists all the options the program was run with in a single string. 1616 1617 Not Collective 1618 1619 Input Parameter: 1620 . options - the options database, use NULL for the default global database 1621 1622 Output Parameter: 1623 . copts - pointer where string pointer is stored 1624 1625 Notes: 1626 The array and each entry in the array should be freed with PetscFree() 1627 Each process may have different values depending on how the options were inserted into the database 1628 1629 Level: advanced 1630 1631 .seealso: PetscOptionsAllUsed(), PetscOptionsView(), PetscOptionsPush(), PetscOptionsPop() 1632 @*/ 1633 PetscErrorCode PetscOptionsGetAll(PetscOptions options,char *copts[]) 1634 { 1635 PetscErrorCode ierr; 1636 PetscInt i; 1637 size_t len = 1,lent = 0; 1638 char *coptions = NULL; 1639 1640 PetscFunctionBegin; 1641 PetscValidPointer(copts,2); 1642 options = options ? options : defaultoptions; 1643 /* count the length of the required string */ 1644 for (i=0; i<options->N; i++) { 1645 ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr); 1646 len += 2 + lent; 1647 if (options->values[i]) { 1648 ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr); 1649 len += 1 + lent; 1650 } 1651 } 1652 ierr = PetscMalloc1(len,&coptions);CHKERRQ(ierr); 1653 coptions[0] = 0; 1654 for (i=0; i<options->N; i++) { 1655 ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr); 1656 ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr); 1657 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 1658 if (options->values[i]) { 1659 ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr); 1660 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 1661 } 1662 } 1663 *copts = coptions; 1664 PetscFunctionReturn(0); 1665 } 1666 1667 /*@C 1668 PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database 1669 1670 Not Collective 1671 1672 Input Parameter: 1673 + options - options database, use NULL for default global database 1674 - name - string name of option 1675 1676 Output Parameter: 1677 . used - PETSC_TRUE if the option was used, otherwise false, including if option was not found in options database 1678 1679 Level: advanced 1680 1681 Notes: 1682 The value returned may be different on each process and depends on which options have been processed 1683 on the given process 1684 1685 .seealso: PetscOptionsView(), PetscOptionsLeft(), PetscOptionsAllUsed() 1686 @*/ 1687 PetscErrorCode PetscOptionsUsed(PetscOptions options,const char *name,PetscBool *used) 1688 { 1689 PetscInt i; 1690 PetscErrorCode ierr; 1691 1692 PetscFunctionBegin; 1693 PetscValidCharPointer(name,2); 1694 PetscValidPointer(used,3); 1695 options = options ? options : defaultoptions; 1696 *used = PETSC_FALSE; 1697 for (i=0; i<options->N; i++) { 1698 ierr = PetscStrcasecmp(options->names[i],name,used);CHKERRQ(ierr); 1699 if (*used) { 1700 *used = options->used[i]; 1701 break; 1702 } 1703 } 1704 PetscFunctionReturn(0); 1705 } 1706 1707 /*@ 1708 PetscOptionsAllUsed - Returns a count of the number of options in the 1709 database that have never been selected. 1710 1711 Not Collective 1712 1713 Input Parameter: 1714 . options - options database, use NULL for default global database 1715 1716 Output Parameter: 1717 . N - count of options not used 1718 1719 Level: advanced 1720 1721 Notes: 1722 The value returned may be different on each process and depends on which options have been processed 1723 on the given process 1724 1725 .seealso: PetscOptionsView() 1726 @*/ 1727 PetscErrorCode PetscOptionsAllUsed(PetscOptions options,PetscInt *N) 1728 { 1729 PetscInt i,n = 0; 1730 1731 PetscFunctionBegin; 1732 PetscValidIntPointer(N,2); 1733 options = options ? options : defaultoptions; 1734 for (i=0; i<options->N; i++) { 1735 if (!options->used[i]) n++; 1736 } 1737 *N = n; 1738 PetscFunctionReturn(0); 1739 } 1740 1741 /*@ 1742 PetscOptionsLeft - Prints to screen any options that were set and never used. 1743 1744 Not Collective 1745 1746 Input Parameter: 1747 . options - options database; use NULL for default global database 1748 1749 Options Database Key: 1750 . -options_left - activates PetscOptionsAllUsed() within PetscFinalize() 1751 1752 Notes: 1753 This is rarely used directly, it is called by PetscFinalize() in debug more or if -options_left 1754 is passed otherwise to help users determine possible mistakes in their usage of options. This 1755 only prints values on process zero of PETSC_COMM_WORLD. Other processes depending the objects 1756 used may have different options that are left unused. 1757 1758 Level: advanced 1759 1760 .seealso: PetscOptionsAllUsed() 1761 @*/ 1762 PetscErrorCode PetscOptionsLeft(PetscOptions options) 1763 { 1764 PetscErrorCode ierr; 1765 PetscInt i; 1766 PetscInt cnt = 0; 1767 PetscOptions toptions; 1768 1769 PetscFunctionBegin; 1770 toptions = options ? options : defaultoptions; 1771 for (i=0; i<toptions->N; i++) { 1772 if (!toptions->used[i]) { 1773 if (toptions->values[i]) { 1774 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",toptions->names[i],toptions->values[i]);CHKERRQ(ierr); 1775 } else { 1776 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s (no value)\n",toptions->names[i]);CHKERRQ(ierr); 1777 } 1778 } 1779 } 1780 if (!options) { 1781 toptions = defaultoptions; 1782 while (toptions->previous) { 1783 cnt++; 1784 toptions = toptions->previous; 1785 } 1786 if (cnt) { 1787 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: You may have forgotten some calls to PetscOptionsPop(),\n PetscOptionsPop() has been called %D less times than PetscOptionsPush()\n",cnt);CHKERRQ(ierr); 1788 } 1789 } 1790 PetscFunctionReturn(0); 1791 } 1792 1793 /*@C 1794 PetscOptionsLeftGet - Returns all options that were set and never used. 1795 1796 Not Collective 1797 1798 Input Parameter: 1799 . options - options database, use NULL for default global database 1800 1801 Output Parameter: 1802 + N - count of options not used 1803 . names - names of options not used 1804 - values - values of options not used 1805 1806 Level: advanced 1807 1808 Notes: 1809 Users should call PetscOptionsLeftRestore() to free the memory allocated in this routine 1810 Notes: The value returned may be different on each process and depends on which options have been processed 1811 on the given process 1812 1813 .seealso: PetscOptionsAllUsed(), PetscOptionsLeft() 1814 @*/ 1815 PetscErrorCode PetscOptionsLeftGet(PetscOptions options,PetscInt *N,char **names[],char **values[]) 1816 { 1817 PetscErrorCode ierr; 1818 PetscInt i,n; 1819 1820 PetscFunctionBegin; 1821 if (N) PetscValidIntPointer(N,2); 1822 if (names) PetscValidPointer(names,3); 1823 if (values) PetscValidPointer(values,4); 1824 options = options ? options : defaultoptions; 1825 1826 /* The number of unused PETSc options */ 1827 n = 0; 1828 for (i=0; i<options->N; i++) { 1829 if (!options->used[i]) n++; 1830 } 1831 if (N) { *N = n; } 1832 if (names) { ierr = PetscMalloc1(n,names);CHKERRQ(ierr); } 1833 if (values) { ierr = PetscMalloc1(n,values);CHKERRQ(ierr); } 1834 1835 n = 0; 1836 if (names || values) { 1837 for (i=0; i<options->N; i++) { 1838 if (!options->used[i]) { 1839 if (names) (*names)[n] = options->names[i]; 1840 if (values) (*values)[n] = options->values[i]; 1841 n++; 1842 } 1843 } 1844 } 1845 PetscFunctionReturn(0); 1846 } 1847 1848 /*@C 1849 PetscOptionsLeftRestore - Free memory for the unused PETSc options obtained using PetscOptionsLeftGet. 1850 1851 Not Collective 1852 1853 Input Parameter: 1854 + options - options database, use NULL for default global database 1855 . names - names of options not used 1856 - values - values of options not used 1857 1858 Level: advanced 1859 1860 .seealso: PetscOptionsAllUsed(), PetscOptionsLeft(), PetscOptionsLeftGet() 1861 @*/ 1862 PetscErrorCode PetscOptionsLeftRestore(PetscOptions options,PetscInt *N,char **names[],char **values[]) 1863 { 1864 PetscErrorCode ierr; 1865 1866 PetscFunctionBegin; 1867 if (N) PetscValidIntPointer(N,2); 1868 if (names) PetscValidPointer(names,3); 1869 if (values) PetscValidPointer(values,4); 1870 if (N) { *N = 0; } 1871 if (names) { ierr = PetscFree(*names);CHKERRQ(ierr); } 1872 if (values) { ierr = PetscFree(*values);CHKERRQ(ierr); } 1873 PetscFunctionReturn(0); 1874 } 1875 1876 /*@C 1877 PetscOptionsMonitorDefault - Print all options set value events using the supplied PetscViewer. 1878 1879 Logically Collective on ctx 1880 1881 Input Parameters: 1882 + name - option name string 1883 . value - option value string 1884 - ctx - an ASCII viewer or NULL 1885 1886 Level: intermediate 1887 1888 Notes: 1889 If ctx=NULL, PetscPrintf() is used. 1890 The first MPI rank in the PetscViewer viewer actually prints the values, other 1891 processes may have different values set 1892 1893 .seealso: PetscOptionsMonitorSet() 1894 @*/ 1895 PetscErrorCode PetscOptionsMonitorDefault(const char name[],const char value[],void *ctx) 1896 { 1897 PetscErrorCode ierr; 1898 1899 PetscFunctionBegin; 1900 if (ctx) { 1901 PetscViewer viewer = (PetscViewer)ctx; 1902 if (!value) { 1903 ierr = PetscViewerASCIIPrintf(viewer,"Removing option: %s\n",name,value);CHKERRQ(ierr); 1904 } else if (!value[0]) { 1905 ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s (no value)\n",name);CHKERRQ(ierr); 1906 } else { 1907 ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr); 1908 } 1909 } else { 1910 MPI_Comm comm = PETSC_COMM_WORLD; 1911 if (!value) { 1912 ierr = PetscPrintf(comm,"Removing option: %s\n",name,value);CHKERRQ(ierr); 1913 } else if (!value[0]) { 1914 ierr = PetscPrintf(comm,"Setting option: %s (no value)\n",name);CHKERRQ(ierr); 1915 } else { 1916 ierr = PetscPrintf(comm,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr); 1917 } 1918 } 1919 PetscFunctionReturn(0); 1920 } 1921 1922 /*@C 1923 PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that 1924 modified the PETSc options database. 1925 1926 Not Collective 1927 1928 Input Parameters: 1929 + monitor - pointer to function (if this is NULL, it turns off monitoring 1930 . mctx - [optional] context for private data for the 1931 monitor routine (use NULL if no context is desired) 1932 - monitordestroy - [optional] routine that frees monitor context 1933 (may be NULL) 1934 1935 Calling Sequence of monitor: 1936 $ monitor (const char name[], const char value[], void *mctx) 1937 1938 + name - option name string 1939 . value - option value string 1940 - mctx - optional monitoring context, as set by PetscOptionsMonitorSet() 1941 1942 Options Database Keys: 1943 See PetscInitialize() for options related to option database monitoring. 1944 1945 Notes: 1946 The default is to do nothing. To print the name and value of options 1947 being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine, 1948 with a null monitoring context. 1949 1950 Several different monitoring routines may be set by calling 1951 PetscOptionsMonitorSet() multiple times; all will be called in the 1952 order in which they were set. 1953 1954 Level: intermediate 1955 1956 .seealso: PetscOptionsMonitorDefault(), PetscInitialize() 1957 @*/ 1958 PetscErrorCode PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void**)) 1959 { 1960 PetscOptions options = defaultoptions; 1961 1962 PetscFunctionBegin; 1963 if (options->monitorCancel) PetscFunctionReturn(0); 1964 if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set"); 1965 options->monitor[options->numbermonitors] = monitor; 1966 options->monitordestroy[options->numbermonitors] = monitordestroy; 1967 options->monitorcontext[options->numbermonitors++] = (void*)mctx; 1968 PetscFunctionReturn(0); 1969 } 1970 1971 /* 1972 PetscOptionsStringToBool - Converts string to PetscBool, handles cases like "yes", "no", "true", "false", "0", "1", "off", "on". 1973 Empty string is considered as true. 1974 */ 1975 PetscErrorCode PetscOptionsStringToBool(const char value[],PetscBool *a) 1976 { 1977 PetscBool istrue,isfalse; 1978 size_t len; 1979 PetscErrorCode ierr; 1980 1981 PetscFunctionBegin; 1982 /* PetscStrlen() returns 0 for NULL or "" */ 1983 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 1984 if (!len) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 1985 ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr); 1986 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 1987 ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr); 1988 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 1989 ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr); 1990 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 1991 ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr); 1992 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 1993 ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr); 1994 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 1995 ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr); 1996 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 1997 ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr); 1998 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 1999 ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr); 2000 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 2001 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown logical value: %s",value); 2002 } 2003 2004 /* 2005 PetscOptionsStringToInt - Converts a string to an integer value. Handles special cases such as "default" and "decide" 2006 */ 2007 PetscErrorCode PetscOptionsStringToInt(const char name[],PetscInt *a) 2008 { 2009 PetscErrorCode ierr; 2010 size_t len; 2011 PetscBool decide,tdefault,mouse; 2012 2013 PetscFunctionBegin; 2014 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 2015 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 2016 2017 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr); 2018 if (!tdefault) { 2019 ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr); 2020 } 2021 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr); 2022 if (!decide) { 2023 ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr); 2024 } 2025 ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr); 2026 2027 if (tdefault) *a = PETSC_DEFAULT; 2028 else if (decide) *a = PETSC_DECIDE; 2029 else if (mouse) *a = -1; 2030 else { 2031 char *endptr; 2032 long strtolval; 2033 2034 strtolval = strtol(name,&endptr,10); 2035 if ((size_t) (endptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name); 2036 2037 #if defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE_ATOLL) 2038 (void) strtolval; 2039 *a = atoll(name); 2040 #elif defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE___INT64) 2041 (void) strtolval; 2042 *a = _atoi64(name); 2043 #else 2044 *a = (PetscInt)strtolval; 2045 #endif 2046 } 2047 PetscFunctionReturn(0); 2048 } 2049 2050 #if defined(PETSC_USE_REAL___FLOAT128) 2051 #include <quadmath.h> 2052 #endif 2053 2054 static PetscErrorCode PetscStrtod(const char name[],PetscReal *a,char **endptr) 2055 { 2056 PetscFunctionBegin; 2057 #if defined(PETSC_USE_REAL___FLOAT128) 2058 *a = strtoflt128(name,endptr); 2059 #else 2060 *a = (PetscReal)strtod(name,endptr); 2061 #endif 2062 PetscFunctionReturn(0); 2063 } 2064 2065 static PetscErrorCode PetscStrtoz(const char name[],PetscScalar *a,char **endptr,PetscBool *isImaginary) 2066 { 2067 PetscBool hasi = PETSC_FALSE; 2068 char *ptr; 2069 PetscReal strtoval; 2070 PetscErrorCode ierr; 2071 2072 PetscFunctionBegin; 2073 ierr = PetscStrtod(name,&strtoval,&ptr);CHKERRQ(ierr); 2074 if (ptr == name) { 2075 strtoval = 1.; 2076 hasi = PETSC_TRUE; 2077 if (name[0] == 'i') { 2078 ptr++; 2079 } else if (name[0] == '+' && name[1] == 'i') { 2080 ptr += 2; 2081 } else if (name[0] == '-' && name[1] == 'i') { 2082 strtoval = -1.; 2083 ptr += 2; 2084 } 2085 } else if (*ptr == 'i') { 2086 hasi = PETSC_TRUE; 2087 ptr++; 2088 } 2089 *endptr = ptr; 2090 *isImaginary = hasi; 2091 if (hasi) { 2092 #if !defined(PETSC_USE_COMPLEX) 2093 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s contains imaginary but complex not supported ",name); 2094 #else 2095 *a = PetscCMPLX(0.,strtoval); 2096 #endif 2097 } else { 2098 *a = strtoval; 2099 } 2100 PetscFunctionReturn(0); 2101 } 2102 2103 /* 2104 Converts a string to PetscReal value. Handles special cases like "default" and "decide" 2105 */ 2106 PetscErrorCode PetscOptionsStringToReal(const char name[],PetscReal *a) 2107 { 2108 size_t len; 2109 PetscBool match; 2110 char *endptr; 2111 PetscErrorCode ierr; 2112 2113 PetscFunctionBegin; 2114 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 2115 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"String of length zero has no numerical value"); 2116 2117 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&match);CHKERRQ(ierr); 2118 if (!match) { 2119 ierr = PetscStrcasecmp(name,"DEFAULT",&match);CHKERRQ(ierr); 2120 } 2121 if (match) {*a = PETSC_DEFAULT; PetscFunctionReturn(0);} 2122 2123 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&match);CHKERRQ(ierr); 2124 if (!match) { 2125 ierr = PetscStrcasecmp(name,"DECIDE",&match);CHKERRQ(ierr); 2126 } 2127 if (match) {*a = PETSC_DECIDE; PetscFunctionReturn(0);} 2128 2129 ierr = PetscStrtod(name,a,&endptr);CHKERRQ(ierr); 2130 if ((size_t) (endptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value",name); 2131 PetscFunctionReturn(0); 2132 } 2133 2134 PetscErrorCode PetscOptionsStringToScalar(const char name[],PetscScalar *a) 2135 { 2136 PetscBool imag1; 2137 size_t len; 2138 PetscScalar val = 0.; 2139 char *ptr = NULL; 2140 PetscErrorCode ierr; 2141 2142 PetscFunctionBegin; 2143 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 2144 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 2145 ierr = PetscStrtoz(name,&val,&ptr,&imag1);CHKERRQ(ierr); 2146 #if defined(PETSC_USE_COMPLEX) 2147 if ((size_t) (ptr - name) < len) { 2148 PetscBool imag2; 2149 PetscScalar val2; 2150 2151 ierr = PetscStrtoz(ptr,&val2,&ptr,&imag2);CHKERRQ(ierr); 2152 if (imag1 || !imag2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s: must specify imaginary component second",name); 2153 val = PetscCMPLX(PetscRealPart(val),PetscImaginaryPart(val2)); 2154 } 2155 #endif 2156 if ((size_t) (ptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name); 2157 *a = val; 2158 PetscFunctionReturn(0); 2159 } 2160 2161 /*@C 2162 PetscOptionsGetBool - Gets the Logical (true or false) value for a particular 2163 option in the database. 2164 2165 Not Collective 2166 2167 Input Parameters: 2168 + options - options database, use NULL for default global database 2169 . pre - the string to prepend to the name or NULL 2170 - name - the option one is seeking 2171 2172 Output Parameter: 2173 + ivalue - the logical value to return 2174 - set - PETSC_TRUE if found, else PETSC_FALSE 2175 2176 Level: beginner 2177 2178 Notes: 2179 TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE 2180 FALSE, false, NO, no, and 0 all translate to PETSC_FALSE 2181 2182 If the option is given, but no value is provided, then ivalue and set are both given the value PETSC_TRUE. That is -requested_bool 2183 is equivalent to -requested_bool true 2184 2185 If the user does not supply the option at all ivalue is NOT changed. Thus 2186 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2187 2188 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 2189 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsBool(), 2190 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2191 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2192 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2193 PetscOptionsFList(), PetscOptionsEList() 2194 @*/ 2195 PetscErrorCode PetscOptionsGetBool(PetscOptions options,const char pre[],const char name[],PetscBool *ivalue,PetscBool *set) 2196 { 2197 const char *value; 2198 PetscBool flag; 2199 PetscErrorCode ierr; 2200 2201 PetscFunctionBegin; 2202 PetscValidCharPointer(name,3); 2203 if (ivalue) PetscValidIntPointer(ivalue,4); 2204 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 2205 if (flag) { 2206 if (set) *set = PETSC_TRUE; 2207 ierr = PetscOptionsStringToBool(value, &flag);CHKERRQ(ierr); 2208 if (ivalue) *ivalue = flag; 2209 } else { 2210 if (set) *set = PETSC_FALSE; 2211 } 2212 PetscFunctionReturn(0); 2213 } 2214 2215 /*@C 2216 PetscOptionsGetEList - Puts a list of option values that a single one may be selected from 2217 2218 Not Collective 2219 2220 Input Parameters: 2221 + options - options database, use NULL for default global database 2222 . pre - the string to prepend to the name or NULL 2223 . opt - option name 2224 . list - the possible choices (one of these must be selected, anything else is invalid) 2225 - ntext - number of choices 2226 2227 Output Parameter: 2228 + value - the index of the value to return (defaults to zero if the option name is given but no choice is listed) 2229 - set - PETSC_TRUE if found, else PETSC_FALSE 2230 2231 Level: intermediate 2232 2233 Notes: 2234 If the user does not supply the option value is NOT changed. Thus 2235 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2236 2237 See PetscOptionsFList() for when the choices are given in a PetscFunctionList() 2238 2239 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 2240 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2241 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2242 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2243 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2244 PetscOptionsFList(), PetscOptionsEList() 2245 @*/ 2246 PetscErrorCode PetscOptionsGetEList(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscInt ntext,PetscInt *value,PetscBool *set) 2247 { 2248 PetscErrorCode ierr; 2249 size_t alen,len = 0, tlen = 0; 2250 char *svalue; 2251 PetscBool aset,flg = PETSC_FALSE; 2252 PetscInt i; 2253 2254 PetscFunctionBegin; 2255 PetscValidCharPointer(opt,3); 2256 for (i=0; i<ntext; i++) { 2257 ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr); 2258 if (alen > len) len = alen; 2259 tlen += len + 1; 2260 } 2261 len += 5; /* a little extra space for user mistypes */ 2262 ierr = PetscMalloc1(len,&svalue);CHKERRQ(ierr); 2263 ierr = PetscOptionsGetString(options,pre,opt,svalue,len,&aset);CHKERRQ(ierr); 2264 if (aset) { 2265 ierr = PetscEListFind(ntext,list,svalue,value,&flg);CHKERRQ(ierr); 2266 if (!flg) { 2267 char *avail,*pavl; 2268 2269 ierr = PetscMalloc1(tlen,&avail);CHKERRQ(ierr); 2270 pavl = avail; 2271 for (i=0; i<ntext; i++) { 2272 ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr); 2273 ierr = PetscStrcpy(pavl,list[i]);CHKERRQ(ierr); 2274 pavl += alen; 2275 ierr = PetscStrcpy(pavl," ");CHKERRQ(ierr); 2276 pavl += 1; 2277 } 2278 ierr = PetscStrtolower(avail);CHKERRQ(ierr); 2279 SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s. Available options: %s",svalue,pre ? pre : "",opt+1,avail); 2280 } 2281 if (set) *set = PETSC_TRUE; 2282 } else if (set) *set = PETSC_FALSE; 2283 ierr = PetscFree(svalue);CHKERRQ(ierr); 2284 PetscFunctionReturn(0); 2285 } 2286 2287 /*@C 2288 PetscOptionsGetEnum - Gets the enum value for a particular option in the database. 2289 2290 Not Collective 2291 2292 Input Parameters: 2293 + options - options database, use NULL for default global database 2294 . pre - option prefix or NULL 2295 . opt - option name 2296 . list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null 2297 - defaultv - the default (current) value 2298 2299 Output Parameter: 2300 + value - the value to return 2301 - set - PETSC_TRUE if found, else PETSC_FALSE 2302 2303 Level: beginner 2304 2305 Notes: 2306 If the user does not supply the option value is NOT changed. Thus 2307 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2308 2309 List is usually something like PCASMTypes or some other predefined list of enum names 2310 2311 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(), 2312 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 2313 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 2314 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2315 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2316 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2317 PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum() 2318 @*/ 2319 PetscErrorCode PetscOptionsGetEnum(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscEnum *value,PetscBool *set) 2320 { 2321 PetscErrorCode ierr; 2322 PetscInt ntext = 0,tval; 2323 PetscBool fset; 2324 2325 PetscFunctionBegin; 2326 PetscValidCharPointer(opt,3); 2327 while (list[ntext++]) { 2328 if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries"); 2329 } 2330 if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix"); 2331 ntext -= 3; 2332 ierr = PetscOptionsGetEList(options,pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr); 2333 /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */ 2334 if (fset) *value = (PetscEnum)tval; 2335 if (set) *set = fset; 2336 PetscFunctionReturn(0); 2337 } 2338 2339 /*@C 2340 PetscOptionsGetInt - Gets the integer value for a particular option in the database. 2341 2342 Not Collective 2343 2344 Input Parameters: 2345 + options - options database, use NULL for default global database 2346 . pre - the string to prepend to the name or NULL 2347 - name - the option one is seeking 2348 2349 Output Parameter: 2350 + ivalue - the integer value to return 2351 - set - PETSC_TRUE if found, else PETSC_FALSE 2352 2353 Level: beginner 2354 2355 Notes: 2356 If the user does not supply the option ivalue is NOT changed. Thus 2357 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2358 2359 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 2360 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 2361 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), 2362 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2363 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2364 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2365 PetscOptionsFList(), PetscOptionsEList() 2366 @*/ 2367 PetscErrorCode PetscOptionsGetInt(PetscOptions options,const char pre[],const char name[],PetscInt *ivalue,PetscBool *set) 2368 { 2369 const char *value; 2370 PetscErrorCode ierr; 2371 PetscBool flag; 2372 2373 PetscFunctionBegin; 2374 PetscValidCharPointer(name,3); 2375 PetscValidIntPointer(ivalue,4); 2376 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 2377 if (flag) { 2378 if (!value) { 2379 if (set) *set = PETSC_FALSE; 2380 } else { 2381 if (set) *set = PETSC_TRUE; 2382 ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr); 2383 } 2384 } else { 2385 if (set) *set = PETSC_FALSE; 2386 } 2387 PetscFunctionReturn(0); 2388 } 2389 2390 /*@C 2391 PetscOptionsGetReal - Gets the double precision value for a particular 2392 option in the database. 2393 2394 Not Collective 2395 2396 Input Parameters: 2397 + options - options database, use NULL for default global database 2398 . pre - string to prepend to each name or NULL 2399 - name - the option one is seeking 2400 2401 Output Parameter: 2402 + dvalue - the double value to return 2403 - set - PETSC_TRUE if found, PETSC_FALSE if not found 2404 2405 Notes: 2406 If the user does not supply the option dvalue is NOT changed. Thus 2407 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2408 2409 Level: beginner 2410 2411 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2412 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(), 2413 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2414 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2415 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2416 PetscOptionsFList(), PetscOptionsEList() 2417 @*/ 2418 PetscErrorCode PetscOptionsGetReal(PetscOptions options,const char pre[],const char name[],PetscReal *dvalue,PetscBool *set) 2419 { 2420 const char *value; 2421 PetscBool flag; 2422 PetscErrorCode ierr; 2423 2424 PetscFunctionBegin; 2425 PetscValidCharPointer(name,3); 2426 PetscValidRealPointer(dvalue,4); 2427 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 2428 if (flag) { 2429 if (!value) { 2430 if (set) *set = PETSC_FALSE; 2431 } else { 2432 if (set) *set = PETSC_TRUE; 2433 ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr); 2434 } 2435 } else { 2436 if (set) *set = PETSC_FALSE; 2437 } 2438 PetscFunctionReturn(0); 2439 } 2440 2441 /*@C 2442 PetscOptionsGetScalar - Gets the scalar value for a particular 2443 option in the database. 2444 2445 Not Collective 2446 2447 Input Parameters: 2448 + options - options database, use NULL for default global database 2449 . pre - string to prepend to each name or NULL 2450 - name - the option one is seeking 2451 2452 Output Parameter: 2453 + dvalue - the double value to return 2454 - set - PETSC_TRUE if found, else PETSC_FALSE 2455 2456 Level: beginner 2457 2458 Usage: 2459 A complex number 2+3i must be specified with NO spaces 2460 2461 Notes: 2462 If the user does not supply the option dvalue is NOT changed. Thus 2463 you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true. 2464 2465 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2466 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2467 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2468 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2469 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2470 PetscOptionsFList(), PetscOptionsEList() 2471 @*/ 2472 PetscErrorCode PetscOptionsGetScalar(PetscOptions options,const char pre[],const char name[],PetscScalar *dvalue,PetscBool *set) 2473 { 2474 const char *value; 2475 PetscBool flag; 2476 PetscErrorCode ierr; 2477 2478 PetscFunctionBegin; 2479 PetscValidCharPointer(name,3); 2480 PetscValidScalarPointer(dvalue,4); 2481 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 2482 if (flag) { 2483 if (!value) { 2484 if (set) *set = PETSC_FALSE; 2485 } else { 2486 #if !defined(PETSC_USE_COMPLEX) 2487 ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr); 2488 #else 2489 ierr = PetscOptionsStringToScalar(value,dvalue);CHKERRQ(ierr); 2490 #endif 2491 if (set) *set = PETSC_TRUE; 2492 } 2493 } else { /* flag */ 2494 if (set) *set = PETSC_FALSE; 2495 } 2496 PetscFunctionReturn(0); 2497 } 2498 2499 /*@C 2500 PetscOptionsGetString - Gets the string value for a particular option in 2501 the database. 2502 2503 Not Collective 2504 2505 Input Parameters: 2506 + options - options database, use NULL for default global database 2507 . pre - string to prepend to name or NULL 2508 . name - the option one is seeking 2509 - len - maximum length of the string including null termination 2510 2511 Output Parameters: 2512 + string - location to copy string 2513 - set - PETSC_TRUE if found, else PETSC_FALSE 2514 2515 Level: beginner 2516 2517 Fortran Note: 2518 The Fortran interface is slightly different from the C/C++ 2519 interface (len is not used). Sample usage in Fortran follows 2520 .vb 2521 character *20 string 2522 PetscErrorCode ierr 2523 PetscBool set 2524 call PetscOptionsGetString(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-s',string,set,ierr) 2525 .ve 2526 2527 Notes: 2528 if the option is given but no string is provided then an empty string is returned and set is given the value of PETSC_TRUE 2529 2530 If the user does not use the option then the string is not changed. Thus 2531 you should ALWAYS initialize the string if you access it without first checking if the set flag is true. 2532 2533 Note: 2534 Even if the user provided no string (for example -optionname -someotheroption) the flag is set to PETSC_TRUE (and the string is fulled with nulls). 2535 2536 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 2537 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2538 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2539 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2540 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2541 PetscOptionsFList(), PetscOptionsEList() 2542 @*/ 2543 PetscErrorCode PetscOptionsGetString(PetscOptions options,const char pre[],const char name[],char string[],size_t len,PetscBool *set) 2544 { 2545 const char *value; 2546 PetscBool flag; 2547 PetscErrorCode ierr; 2548 2549 PetscFunctionBegin; 2550 PetscValidCharPointer(name,3); 2551 PetscValidCharPointer(string,4); 2552 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr); 2553 if (!flag) { 2554 if (set) *set = PETSC_FALSE; 2555 } else { 2556 if (set) *set = PETSC_TRUE; 2557 if (value) { 2558 ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr); 2559 } else { 2560 ierr = PetscArrayzero(string,len);CHKERRQ(ierr); 2561 } 2562 } 2563 PetscFunctionReturn(0); 2564 } 2565 2566 char *PetscOptionsGetStringMatlab(PetscOptions options,const char pre[],const char name[]) 2567 { 2568 const char *value; 2569 PetscBool flag; 2570 PetscErrorCode ierr; 2571 2572 PetscFunctionBegin; 2573 ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);if (ierr) PetscFunctionReturn(NULL); 2574 if (flag) PetscFunctionReturn((char*)value); 2575 else PetscFunctionReturn(NULL); 2576 } 2577 2578 /*@C 2579 PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular 2580 option in the database. The values must be separated with commas with 2581 no intervening spaces. 2582 2583 Not Collective 2584 2585 Input Parameters: 2586 + options - options database, use NULL for default global database 2587 . pre - string to prepend to each name or NULL 2588 . name - the option one is seeking 2589 - nmax - maximum number of values to retrieve 2590 2591 Output Parameter: 2592 + dvalue - the integer values to return 2593 . nmax - actual number of values retreived 2594 - set - PETSC_TRUE if found, else PETSC_FALSE 2595 2596 Level: beginner 2597 2598 Notes: 2599 TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE 2600 FALSE, false, NO, no, and 0 all translate to PETSC_FALSE 2601 2602 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2603 PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2604 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2605 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2606 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2607 PetscOptionsFList(), PetscOptionsEList() 2608 @*/ 2609 PetscErrorCode PetscOptionsGetBoolArray(PetscOptions options,const char pre[],const char name[],PetscBool dvalue[],PetscInt *nmax,PetscBool *set) 2610 { 2611 const char *svalue; 2612 char *value; 2613 PetscErrorCode ierr; 2614 PetscInt n = 0; 2615 PetscBool flag; 2616 PetscToken token; 2617 2618 PetscFunctionBegin; 2619 PetscValidCharPointer(name,3); 2620 PetscValidIntPointer(dvalue,4); 2621 PetscValidIntPointer(nmax,5); 2622 2623 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2624 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2625 if (set) *set = PETSC_TRUE; 2626 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2627 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2628 while (value && n < *nmax) { 2629 ierr = PetscOptionsStringToBool(value,dvalue);CHKERRQ(ierr); 2630 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2631 dvalue++; 2632 n++; 2633 } 2634 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2635 *nmax = n; 2636 PetscFunctionReturn(0); 2637 } 2638 2639 /*@C 2640 PetscOptionsGetEnumArray - Gets an array of enum values for a particular option in the database. 2641 2642 Not Collective 2643 2644 Input Parameters: 2645 + options - options database, use NULL for default global database 2646 . pre - option prefix or NULL 2647 . name - option name 2648 . list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null 2649 - nmax - maximum number of values to retrieve 2650 2651 Output Parameters: 2652 + ivalue - the enum values to return 2653 . nmax - actual number of values retreived 2654 - set - PETSC_TRUE if found, else PETSC_FALSE 2655 2656 Level: beginner 2657 2658 Notes: 2659 The array must be passed as a comma separated list. 2660 2661 There must be no intervening spaces between the values. 2662 2663 list is usually something like PCASMTypes or some other predefined list of enum names. 2664 2665 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(), 2666 PetscOptionsGetEnum(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool() 2667 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), PetscOptionsName(), 2668 PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), PetscOptionsStringArray(),PetscOptionsRealArray(), 2669 PetscOptionsScalar(), PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2670 PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum() 2671 @*/ 2672 PetscErrorCode PetscOptionsGetEnumArray(PetscOptions options,const char pre[],const char name[],const char *const *list,PetscEnum ivalue[],PetscInt *nmax,PetscBool *set) 2673 { 2674 const char *svalue; 2675 char *value; 2676 PetscInt n = 0; 2677 PetscEnum evalue; 2678 PetscBool flag; 2679 PetscToken token; 2680 PetscErrorCode ierr; 2681 2682 PetscFunctionBegin; 2683 PetscValidCharPointer(name,3); 2684 PetscValidPointer(list,4); 2685 PetscValidPointer(ivalue,5); 2686 PetscValidIntPointer(nmax,6); 2687 2688 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2689 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2690 if (set) *set = PETSC_TRUE; 2691 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2692 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2693 while (value && n < *nmax) { 2694 ierr = PetscEnumFind(list,value,&evalue,&flag);CHKERRQ(ierr); 2695 if (!flag) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown enum value '%s' for -%s%s",svalue,pre ? pre : "",name+1); 2696 ivalue[n++] = evalue; 2697 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2698 } 2699 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2700 *nmax = n; 2701 PetscFunctionReturn(0); 2702 } 2703 2704 /*@C 2705 PetscOptionsGetIntArray - Gets an array of integer values for a particular 2706 option in the database. 2707 2708 Not Collective 2709 2710 Input Parameters: 2711 + options - options database, use NULL for default global database 2712 . pre - string to prepend to each name or NULL 2713 . name - the option one is seeking 2714 - nmax - maximum number of values to retrieve 2715 2716 Output Parameter: 2717 + ivalue - the integer values to return 2718 . nmax - actual number of values retreived 2719 - set - PETSC_TRUE if found, else PETSC_FALSE 2720 2721 Level: beginner 2722 2723 Notes: 2724 The array can be passed as 2725 a comma separated list: 0,1,2,3,4,5,6,7 2726 a range (start-end+1): 0-8 2727 a range with given increment (start-end+1:inc): 0-7:2 2728 a combination of values and ranges separated by commas: 0,1-8,8-15:2 2729 2730 There must be no intervening spaces between the values. 2731 2732 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2733 PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2734 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2735 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2736 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2737 PetscOptionsFList(), PetscOptionsEList() 2738 @*/ 2739 PetscErrorCode PetscOptionsGetIntArray(PetscOptions options,const char pre[],const char name[],PetscInt ivalue[],PetscInt *nmax,PetscBool *set) 2740 { 2741 const char *svalue; 2742 char *value; 2743 PetscErrorCode ierr; 2744 PetscInt n = 0,i,j,start,end,inc,nvalues; 2745 size_t len; 2746 PetscBool flag,foundrange; 2747 PetscToken token; 2748 2749 PetscFunctionBegin; 2750 PetscValidCharPointer(name,3); 2751 PetscValidIntPointer(ivalue,4); 2752 PetscValidIntPointer(nmax,5); 2753 2754 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2755 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2756 if (set) *set = PETSC_TRUE; 2757 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2758 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2759 while (value && n < *nmax) { 2760 /* look for form d-D where d and D are integers */ 2761 foundrange = PETSC_FALSE; 2762 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 2763 if (value[0] == '-') i=2; 2764 else i=1; 2765 for (;i<(int)len; i++) { 2766 if (value[i] == '-') { 2767 if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value); 2768 value[i] = 0; 2769 2770 ierr = PetscOptionsStringToInt(value,&start);CHKERRQ(ierr); 2771 inc = 1; 2772 j = i+1; 2773 for (;j<(int)len; j++) { 2774 if (value[j] == ':') { 2775 value[j] = 0; 2776 2777 ierr = PetscOptionsStringToInt(value+j+1,&inc);CHKERRQ(ierr); 2778 if (inc <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry,%s cannot have negative increment",n,value+j+1); 2779 break; 2780 } 2781 } 2782 ierr = PetscOptionsStringToInt(value+i+1,&end);CHKERRQ(ierr); 2783 if (end <= start) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1); 2784 nvalues = (end-start)/inc + (end-start)%inc; 2785 if (n + nvalues > *nmax) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, not enough space left in array (%D) to contain entire range from %D to %D",n,*nmax-n,start,end); 2786 for (;start<end; start+=inc) { 2787 *ivalue = start; ivalue++;n++; 2788 } 2789 foundrange = PETSC_TRUE; 2790 break; 2791 } 2792 } 2793 if (!foundrange) { 2794 ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr); 2795 ivalue++; 2796 n++; 2797 } 2798 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2799 } 2800 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2801 *nmax = n; 2802 PetscFunctionReturn(0); 2803 } 2804 2805 /*@C 2806 PetscOptionsGetRealArray - Gets an array of double precision values for a 2807 particular option in the database. The values must be separated with 2808 commas with no intervening spaces. 2809 2810 Not Collective 2811 2812 Input Parameters: 2813 + options - options database, use NULL for default global database 2814 . pre - string to prepend to each name or NULL 2815 . name - the option one is seeking 2816 - nmax - maximum number of values to retrieve 2817 2818 Output Parameters: 2819 + dvalue - the double values to return 2820 . nmax - actual number of values retreived 2821 - set - PETSC_TRUE if found, else PETSC_FALSE 2822 2823 Level: beginner 2824 2825 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2826 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(), 2827 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2828 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2829 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2830 PetscOptionsFList(), PetscOptionsEList() 2831 @*/ 2832 PetscErrorCode PetscOptionsGetRealArray(PetscOptions options,const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscBool *set) 2833 { 2834 const char *svalue; 2835 char *value; 2836 PetscErrorCode ierr; 2837 PetscInt n = 0; 2838 PetscBool flag; 2839 PetscToken token; 2840 2841 PetscFunctionBegin; 2842 PetscValidCharPointer(name,3); 2843 PetscValidRealPointer(dvalue,4); 2844 PetscValidIntPointer(nmax,5); 2845 2846 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2847 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2848 if (set) *set = PETSC_TRUE; 2849 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2850 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2851 while (value && n < *nmax) { 2852 ierr = PetscOptionsStringToReal(value,dvalue++);CHKERRQ(ierr); 2853 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2854 n++; 2855 } 2856 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2857 *nmax = n; 2858 PetscFunctionReturn(0); 2859 } 2860 2861 /*@C 2862 PetscOptionsGetScalarArray - Gets an array of scalars for a 2863 particular option in the database. The values must be separated with 2864 commas with no intervening spaces. 2865 2866 Not Collective 2867 2868 Input Parameters: 2869 + options - options database, use NULL for default global database 2870 . pre - string to prepend to each name or NULL 2871 . name - the option one is seeking 2872 - nmax - maximum number of values to retrieve 2873 2874 Output Parameters: 2875 + dvalue - the scalar values to return 2876 . nmax - actual number of values retreived 2877 - set - PETSC_TRUE if found, else PETSC_FALSE 2878 2879 Level: beginner 2880 2881 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 2882 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(), 2883 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2884 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2885 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2886 PetscOptionsFList(), PetscOptionsEList() 2887 @*/ 2888 PetscErrorCode PetscOptionsGetScalarArray(PetscOptions options,const char pre[],const char name[],PetscScalar dvalue[],PetscInt *nmax,PetscBool *set) 2889 { 2890 const char *svalue; 2891 char *value; 2892 PetscErrorCode ierr; 2893 PetscInt n = 0; 2894 PetscBool flag; 2895 PetscToken token; 2896 2897 PetscFunctionBegin; 2898 PetscValidCharPointer(name,3); 2899 PetscValidRealPointer(dvalue,4); 2900 PetscValidIntPointer(nmax,5); 2901 2902 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2903 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2904 if (set) *set = PETSC_TRUE; 2905 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2906 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2907 while (value && n < *nmax) { 2908 ierr = PetscOptionsStringToScalar(value,dvalue++);CHKERRQ(ierr); 2909 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2910 n++; 2911 } 2912 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2913 *nmax = n; 2914 PetscFunctionReturn(0); 2915 } 2916 2917 /*@C 2918 PetscOptionsGetStringArray - Gets an array of string values for a particular 2919 option in the database. The values must be separated with commas with 2920 no intervening spaces. 2921 2922 Not Collective 2923 2924 Input Parameters: 2925 + options - options database, use NULL for default global database 2926 . pre - string to prepend to name or NULL 2927 . name - the option one is seeking 2928 - nmax - maximum number of strings 2929 2930 Output Parameters: 2931 + strings - location to copy strings 2932 . nmax - the number of strings found 2933 - set - PETSC_TRUE if found, else PETSC_FALSE 2934 2935 Level: beginner 2936 2937 Notes: 2938 The nmax parameter is used for both input and output. 2939 2940 The user should pass in an array of pointers to char, to hold all the 2941 strings returned by this function. 2942 2943 The user is responsible for deallocating the strings that are 2944 returned. The Fortran interface for this routine is not supported. 2945 2946 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 2947 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(), 2948 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 2949 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 2950 PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(), 2951 PetscOptionsFList(), PetscOptionsEList() 2952 @*/ 2953 PetscErrorCode PetscOptionsGetStringArray(PetscOptions options,const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscBool *set) 2954 { 2955 const char *svalue; 2956 char *value; 2957 PetscErrorCode ierr; 2958 PetscInt n = 0; 2959 PetscBool flag; 2960 PetscToken token; 2961 2962 PetscFunctionBegin; 2963 PetscValidCharPointer(name,3); 2964 PetscValidPointer(strings,4); 2965 PetscValidIntPointer(nmax,5); 2966 2967 ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr); 2968 if (!flag || !svalue) { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 2969 if (set) *set = PETSC_TRUE; 2970 ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr); 2971 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2972 while (value && n < *nmax) { 2973 ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr); 2974 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 2975 n++; 2976 } 2977 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 2978 *nmax = n; 2979 PetscFunctionReturn(0); 2980 } 2981 2982 /*@C 2983 PetscOptionsDeprecated - mark an option as deprecated, optionally replacing it with a new one 2984 2985 Prints a deprecation warning, unless an option is supplied to suppress. 2986 2987 Logically Collective 2988 2989 Input Parameters: 2990 + pre - string to prepend to name or NULL 2991 . oldname - the old, deprecated option 2992 . newname - the new option, or NULL if option is purely removed 2993 . version - a string describing the version of first deprecation, e.g. "3.9" 2994 - info - additional information string, or NULL. 2995 2996 Options Database Keys: 2997 . -options_suppress_deprecated_warnings - do not print deprecation warnings 2998 2999 Notes: 3000 Must be called between PetscOptionsBegin() (or PetscObjectOptionsBegin()) and PetscOptionsEnd(). 3001 Only the proces of rank zero that owns the PetscOptionsItems are argument (managed by PetscOptionsBegin() or 3002 PetscObjectOptionsBegin() prints the information 3003 If newname is provided, the old option is replaced. Otherwise, it remains 3004 in the options database. 3005 If an option is not replaced, the info argument should be used to advise the user 3006 on how to proceed. 3007 There is a limit on the length of the warning printed, so very long strings 3008 provided as info may be truncated. 3009 3010 Level: developer 3011 3012 .seealso: PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsScalar(), PetscOptionsBool(), PetscOptionsString(), PetscOptionsSetValue() 3013 3014 @*/ 3015 PetscErrorCode PetscOptionsDeprecated_Private(PetscOptionItems *PetscOptionsObject,const char oldname[],const char newname[],const char version[],const char info[]) 3016 { 3017 PetscErrorCode ierr; 3018 PetscBool found,quiet; 3019 const char *value; 3020 const char * const quietopt="-options_suppress_deprecated_warnings"; 3021 char msg[4096]; 3022 char *prefix = NULL; 3023 PetscOptions options = NULL; 3024 MPI_Comm comm = PETSC_COMM_SELF; 3025 3026 PetscFunctionBegin; 3027 PetscValidCharPointer(oldname,2); 3028 PetscValidCharPointer(version,4); 3029 if (PetscOptionsObject) { 3030 prefix = PetscOptionsObject->prefix; 3031 options = PetscOptionsObject->options; 3032 comm = PetscOptionsObject->comm; 3033 } 3034 ierr = PetscOptionsFindPair(options,prefix,oldname,&value,&found);CHKERRQ(ierr); 3035 if (found) { 3036 if (newname) { 3037 if (prefix) { 3038 ierr = PetscOptionsPrefixPush(options,prefix);CHKERRQ(ierr); 3039 } 3040 ierr = PetscOptionsSetValue(options,newname,value);CHKERRQ(ierr); 3041 if (prefix) { 3042 ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr); 3043 } 3044 ierr = PetscOptionsClearValue(options,oldname);CHKERRQ(ierr); 3045 } 3046 quiet = PETSC_FALSE; 3047 ierr = PetscOptionsGetBool(options,NULL,quietopt,&quiet,NULL);CHKERRQ(ierr); 3048 if (!quiet) { 3049 ierr = PetscStrcpy(msg,"** PETSc DEPRECATION WARNING ** : the option ");CHKERRQ(ierr); 3050 ierr = PetscStrcat(msg,oldname);CHKERRQ(ierr); 3051 ierr = PetscStrcat(msg," is deprecated as of version ");CHKERRQ(ierr); 3052 ierr = PetscStrcat(msg,version);CHKERRQ(ierr); 3053 ierr = PetscStrcat(msg," and will be removed in a future release.");CHKERRQ(ierr); 3054 if (newname) { 3055 ierr = PetscStrcat(msg," Please use the option ");CHKERRQ(ierr); 3056 ierr = PetscStrcat(msg,newname);CHKERRQ(ierr); 3057 ierr = PetscStrcat(msg," instead.");CHKERRQ(ierr); 3058 } 3059 if (info) { 3060 ierr = PetscStrcat(msg," ");CHKERRQ(ierr); 3061 ierr = PetscStrcat(msg,info);CHKERRQ(ierr); 3062 } 3063 ierr = PetscStrcat(msg," (Silence this warning with ");CHKERRQ(ierr); 3064 ierr = PetscStrcat(msg,quietopt);CHKERRQ(ierr); 3065 ierr = PetscStrcat(msg,")\n");CHKERRQ(ierr); 3066 ierr = PetscPrintf(comm,msg);CHKERRQ(ierr); 3067 } 3068 } 3069 PetscFunctionReturn(0); 3070 } 3071