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