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