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