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