1 #define PETSC_DLL 2 /* 3 These routines simplify the use of command line, file options, etc., and are used to manipulate the options database. 4 This provides the low-level interface, the high level interface is in aoptions.c 5 6 Some routines use regular malloc and free because it cannot know what malloc is requested with the 7 options database until it has already processed the input. 8 */ 9 10 #include "petscsys.h" /*I "petscsys.h" I*/ 11 #if defined(PETSC_HAVE_STDLIB_H) 12 #include <stdlib.h> 13 #endif 14 #if defined(PETSC_HAVE_MALLOC_H) 15 #include <malloc.h> 16 #endif 17 #if defined(PETSC_HAVE_SYS_PARAM_H) 18 #include "sys/param.h" 19 #endif 20 21 /* 22 This table holds all the options set by the user. For simplicity, we use a static size database 23 */ 24 #define MAXOPTIONS 512 25 #define MAXALIASES 25 26 #define MAXOPTIONSMONITORS 5 27 28 typedef struct { 29 int N,argc,Naliases; 30 char **args,*names[MAXOPTIONS],*values[MAXOPTIONS]; 31 char *aliases1[MAXALIASES],*aliases2[MAXALIASES]; 32 PetscTruth used[MAXOPTIONS]; 33 PetscTruth namegiven; 34 char programname[PETSC_MAX_PATH_LEN]; /* HP includes entire path in name */ 35 36 /* --------User (or default) routines (most return -1 on error) --------*/ 37 PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[], const char[], void*); /* returns control to user after */ 38 PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void*); /* */ 39 void *monitorcontext[MAXOPTIONSMONITORS]; /* to pass arbitrary user data into monitor */ 40 PetscInt numbermonitors; /* to, for instance, detect options being set */ 41 42 } PetscOptionsTable; 43 44 45 static PetscOptionsTable *options = 0; 46 extern PetscOptionsObjectType PetscOptionsObject; 47 48 /* 49 Options events monitor 50 */ 51 #define PetscOptionsMonitor(name,value) \ 52 { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \ 53 for (_i=0; _i<_im; _i++) {\ 54 _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \ 55 } \ 56 } 57 58 #undef __FUNCT__ 59 #define __FUNCT__ "PetscOptionsAtoi" 60 /* 61 PetscOptionsAtoi - Converts a string to an integer value. Handles special cases such as "default" and "decide" 62 */ 63 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtoi(const char name[],PetscInt *a) 64 { 65 PetscErrorCode ierr; 66 size_t i,len; 67 PetscTruth decide,tdefault,mouse; 68 69 PetscFunctionBegin; 70 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 71 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 72 73 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr); 74 if (!tdefault) { 75 ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr); 76 } 77 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr); 78 if (!decide) { 79 ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr); 80 } 81 ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr); 82 83 if (tdefault) { 84 *a = PETSC_DEFAULT; 85 } else if (decide) { 86 *a = PETSC_DECIDE; 87 } else if (mouse) { 88 *a = -1; 89 } else { 90 if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') { 91 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name); 92 } 93 for (i=1; i<len; i++) { 94 if (name[i] < '0' || name[i] > '9') { 95 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name); 96 } 97 } 98 *a = atoi(name); 99 } 100 PetscFunctionReturn(0); 101 } 102 103 #undef __FUNCT__ 104 #define __FUNCT__ "PetscOptionsAtod" 105 /* 106 Converts a string to PetscReal value. Handles special cases like "default" and "decide" 107 */ 108 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtod(const char name[],PetscReal *a) 109 { 110 PetscErrorCode ierr; 111 size_t len; 112 PetscTruth decide,tdefault; 113 114 PetscFunctionBegin; 115 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 116 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value"); 117 118 ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr); 119 if (!tdefault) { 120 ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr); 121 } 122 ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr); 123 if (!decide) { 124 ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr); 125 } 126 127 if (tdefault) { 128 *a = PETSC_DEFAULT; 129 } else if (decide) { 130 *a = PETSC_DECIDE; 131 } else { 132 if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') { 133 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name); 134 } 135 *a = atof(name); 136 } 137 PetscFunctionReturn(0); 138 } 139 140 #undef __FUNCT__ 141 #define __FUNCT__ "PetscOptionsAtol" 142 /* 143 PetscOptionsAtol - Converts string to PetscTruth, handles cases like "yes", "no", "true", "false", "0", "1" 144 */ 145 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtol(const char value[], PetscTruth *a) 146 { 147 PetscTruth istrue, isfalse; 148 size_t len; 149 PetscErrorCode ierr; 150 151 PetscFunctionBegin; 152 ierr = PetscStrlen(value, &len);CHKERRQ(ierr); 153 if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Character string of length zero has no logical value"); 154 ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr); 155 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 156 ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr); 157 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 158 ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr); 159 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 160 ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr); 161 if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);} 162 ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr); 163 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 164 ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr); 165 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 166 ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr); 167 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 168 ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr); 169 if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);} 170 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Unknown logical value: %s", value); 171 PetscFunctionReturn(0); 172 } 173 174 #undef __FUNCT__ 175 #define __FUNCT__ "PetscGetProgramName" 176 /*@C 177 PetscGetProgramName - Gets the name of the running program. 178 179 Not Collective 180 181 Input Parameter: 182 . len - length of the string name 183 184 Output Parameter: 185 . name - the name of the running program 186 187 Level: advanced 188 189 Notes: 190 The name of the program is copied into the user-provided character 191 array of length len. On some machines the program name includes 192 its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN. 193 @*/ 194 PetscErrorCode PETSC_DLLEXPORT PetscGetProgramName(char name[],size_t len) 195 { 196 PetscErrorCode ierr; 197 198 PetscFunctionBegin; 199 if (!options) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscInitialize() first"); 200 if (!options->namegiven) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Unable to determine program name"); 201 ierr = PetscStrncpy(name,options->programname,len);CHKERRQ(ierr); 202 PetscFunctionReturn(0); 203 } 204 205 #undef __FUNCT__ 206 #define __FUNCT__ "PetscSetProgramName" 207 PetscErrorCode PETSC_DLLEXPORT PetscSetProgramName(const char name[]) 208 { 209 PetscErrorCode ierr; 210 211 PetscFunctionBegin; 212 options->namegiven = PETSC_TRUE; 213 ierr = PetscStrncpy(options->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 214 PetscFunctionReturn(0); 215 } 216 217 #undef __FUNCT__ 218 #define __FUNCT__ "PetscOptionsValidKey" 219 /*@ 220 PetscOptionsValidKey - PETSc Options database keys must begin with a - followed by a letter. 221 222 Input Parameter: 223 . in_str - string to check if valid 224 225 Output Parameter: 226 . key - PETSC_TRUE if a valid key 227 228 Level: intermediate 229 230 @*/ 231 PetscErrorCode PETSC_DLLEXPORT PetscOptionsValidKey(const char in_str[],PetscTruth *key) 232 { 233 PetscFunctionBegin; 234 *key = PETSC_FALSE; 235 if (!in_str) PetscFunctionReturn(0); 236 if (in_str[0] != '-') PetscFunctionReturn(0); 237 if ((in_str[1] < 'A') || (in_str[1] > 'z')) PetscFunctionReturn(0); 238 *key = PETSC_TRUE; 239 PetscFunctionReturn(0); 240 } 241 242 #undef __FUNCT__ 243 #define __FUNCT__ "PetscOptionsInsertString" 244 /*@C 245 PetscOptionsInsertString - Inserts options into the database from a string 246 247 Not collective: but only processes that call this routine will set the options 248 included in the file 249 250 Input Parameter: 251 . in_str - string that contains options separated by blanks 252 253 254 Level: intermediate 255 256 Contributed by Boyana Norris 257 258 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(), 259 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 260 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 261 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 262 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 263 PetscOptionsList(), PetscOptionsEList(), PetscOptionsInsertFile() 264 265 @*/ 266 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertString(const char in_str[]) 267 { 268 char *first,*second; 269 PetscErrorCode ierr; 270 PetscToken token; 271 PetscTruth key; 272 273 PetscFunctionBegin; 274 ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr); 275 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 276 while (first) { 277 ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr); 278 if (key) { 279 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 280 ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr); 281 if (!key) { 282 ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr); 283 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 284 } else { 285 ierr = PetscOptionsSetValue(first,PETSC_NULL);CHKERRQ(ierr); 286 first = second; 287 } 288 } else { 289 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 290 } 291 } 292 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 293 PetscFunctionReturn(0); 294 } 295 296 /* 297 Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free() 298 */ 299 static char *Petscgetline(FILE * f) 300 { 301 size_t size = 0; 302 size_t len = 0; 303 size_t last = 0; 304 char * buf = PETSC_NULL; 305 306 if (feof(f)) return 0; 307 do { 308 size += 1024; /* BUFSIZ is defined as "the optimal read size for this platform" */ 309 buf = (char*)realloc((void *)buf,size); /* realloc(NULL,n) is the same as malloc(n) */ 310 /* Actually do the read. Note that fgets puts a terminal '\0' on the 311 end of the string, so we make sure we overwrite this */ 312 if (!fgets(buf+len,size,f)) buf[len]=0; 313 PetscStrlen(buf,&len); 314 last = len - 1; 315 } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r'); 316 if (len) return buf; 317 free(buf); 318 return 0; 319 } 320 321 322 #undef __FUNCT__ 323 #define __FUNCT__ "PetscOptionsInsertFile" 324 /*@C 325 PetscOptionsInsertFile - Inserts options into the database from a file. 326 327 Collective on MPI_Comm 328 329 Input Parameter: 330 + comm - the processes that will share the options (usually PETSC_COMM_WORLD) 331 . file - name of file 332 - require - if PETSC_TRUE will generate an error if the file does not exist 333 334 335 Level: intermediate 336 337 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(), 338 PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 339 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 340 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 341 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 342 PetscOptionsList(), PetscOptionsEList() 343 344 @*/ 345 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertFile(MPI_Comm comm,const char file[],PetscTruth require) 346 { 347 char *string,fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*vstring = 0,*astring = 0; 348 PetscErrorCode ierr; 349 size_t i,len; 350 FILE *fd; 351 PetscToken token; 352 int err; 353 char cmt[3]={'#','!','%'},*cmatch; 354 PetscMPIInt rank,cnt=0,acnt=0; 355 356 PetscFunctionBegin; 357 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 358 if (!rank) { 359 /* Warning: assume a maximum size for all options in a string */ 360 ierr = PetscMalloc(128000*sizeof(char),&vstring);CHKERRQ(ierr); 361 vstring[0] = 0; 362 ierr = PetscMalloc(64000*sizeof(char),&astring);CHKERRQ(ierr); 363 astring[0] = 0; 364 cnt = 0; 365 acnt = 0; 366 367 ierr = PetscFixFilename(file,fname);CHKERRQ(ierr); 368 fd = fopen(fname,"r"); 369 if (fd) { 370 /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */ 371 ierr = PetscInfo1(0,"Opened options file %s\n",file);CHKERRQ(ierr); 372 while ((string = Petscgetline(fd))) { 373 /* eliminate comments from each line */ 374 for (i=0; i<3; i++){ 375 ierr = PetscStrchr(string,cmt[i],&cmatch); 376 if (cmatch) *cmatch = 0; 377 } 378 ierr = PetscStrlen(string,&len);CHKERRQ(ierr); 379 /* replace tabs, ^M, \n with " " */ 380 for (i=0; i<len; i++) { 381 if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') { 382 string[i] = ' '; 383 } 384 } 385 ierr = PetscTokenCreate(string,' ',&token);CHKERRQ(ierr); 386 free(string); 387 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 388 if (!first) { 389 goto destroy; 390 } else if (!first[0]) { /* if first token is empty spaces, redo first token */ 391 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 392 } 393 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 394 if (!first) { 395 goto destroy; 396 } else if (first[0] == '-') { 397 /* warning: should be making sure we do not overfill vstring */ 398 ierr = PetscStrcat(vstring,first);CHKERRQ(ierr); 399 ierr = PetscStrcat(vstring," ");CHKERRQ(ierr); 400 if (second) { 401 /* protect second with quotes in case it contains strings */ 402 ierr = PetscStrcat(vstring,"\"");CHKERRQ(ierr); 403 ierr = PetscStrcat(vstring,second);CHKERRQ(ierr); 404 ierr = PetscStrcat(vstring,"\"");CHKERRQ(ierr); 405 } 406 ierr = PetscStrcat(vstring," ");CHKERRQ(ierr); 407 } else { 408 PetscTruth match; 409 410 ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr); 411 if (match) { 412 ierr = PetscTokenFind(token,&third);CHKERRQ(ierr); 413 if (!third) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second); 414 ierr = PetscStrcat(astring,second);CHKERRQ(ierr); 415 ierr = PetscStrcat(astring," ");CHKERRQ(ierr); 416 ierr = PetscStrcat(astring,third);CHKERRQ(ierr); 417 ierr = PetscStrcat(astring," ");CHKERRQ(ierr); 418 } else { 419 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown statement in options file: (%s)",string); 420 } 421 } 422 destroy: 423 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 424 } 425 err = fclose(fd); 426 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 427 ierr = PetscStrlen(astring,&len);CHKERRQ(ierr); 428 acnt = PetscMPIIntCast(len);CHKERRQ(ierr); 429 ierr = PetscStrlen(vstring,&len);CHKERRQ(ierr); 430 cnt = PetscMPIIntCast(len);CHKERRQ(ierr); 431 } else if (require) { 432 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Unable to open Options File %s",fname); 433 } 434 } 435 436 ierr = MPI_Bcast(&acnt,1,MPI_INT,0,comm);CHKERRQ(ierr); 437 if (acnt) { 438 PetscToken token; 439 char *first,*second; 440 441 if (rank) { 442 ierr = PetscMalloc((acnt+1)*sizeof(char),&astring);CHKERRQ(ierr); 443 } 444 ierr = MPI_Bcast(astring,acnt,MPI_CHAR,0,comm);CHKERRQ(ierr); 445 astring[acnt] = 0; 446 ierr = PetscTokenCreate(astring,' ',&token);CHKERRQ(ierr); 447 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 448 while (first) { 449 ierr = PetscTokenFind(token,&second);CHKERRQ(ierr); 450 ierr = PetscOptionsSetAlias(first,second);CHKERRQ(ierr); 451 ierr = PetscTokenFind(token,&first);CHKERRQ(ierr); 452 } 453 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 454 } 455 456 ierr = MPI_Bcast(&cnt,1,MPI_INT,0,comm);CHKERRQ(ierr); 457 if (cnt) { 458 if (rank) { 459 ierr = PetscMalloc((cnt+1)*sizeof(char),&vstring);CHKERRQ(ierr); 460 } 461 ierr = MPI_Bcast(vstring,cnt,MPI_CHAR,0,comm);CHKERRQ(ierr); 462 vstring[cnt] = 0; 463 ierr = PetscOptionsInsertString(vstring);CHKERRQ(ierr); 464 } 465 ierr = PetscFree(astring);CHKERRQ(ierr); 466 ierr = PetscFree(vstring);CHKERRQ(ierr); 467 PetscFunctionReturn(0); 468 } 469 470 #undef __FUNCT__ 471 #define __FUNCT__ "PetscOptionsInsert" 472 /*@C 473 PetscOptionsInsert - Inserts into the options database from the command line, 474 the environmental variable and a file. 475 476 Input Parameters: 477 + argc - count of number of command line arguments 478 . args - the command line arguments 479 - file - optional filename, defaults to ~username/.petscrc 480 481 Note: 482 Since PetscOptionsInsert() is automatically called by PetscInitialize(), 483 the user does not typically need to call this routine. PetscOptionsInsert() 484 can be called several times, adding additional entries into the database. 485 486 Options Database Keys: 487 + -options_monitor <optional filename> - print options names and values as they are set 488 . -options_file <filename> - read options from a file 489 490 Level: advanced 491 492 Concepts: options database^adding 493 494 .seealso: PetscOptionsDestroy_Private(), PetscOptionsPrint(), PetscOptionsInsertString(), PetscOptionsInsertFile(), 495 PetscInitialize() 496 @*/ 497 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsert(int *argc,char ***args,const char file[]) 498 { 499 PetscErrorCode ierr; 500 PetscMPIInt rank; 501 char pfile[PETSC_MAX_PATH_LEN]; 502 PetscTruth flag = PETSC_FALSE; 503 504 PetscFunctionBegin; 505 if (!options) { 506 fprintf(stderr, "Options have not been enabled.\nYou might have forgotten to call PetscInitialize().\n"); 507 MPI_Abort(MPI_COMM_WORLD, PETSC_ERR_SUP); 508 } 509 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 510 511 options->argc = (argc) ? *argc : 0; 512 options->args = (args) ? *args : PETSC_NULL; 513 514 if (file) { 515 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,file,PETSC_TRUE);CHKERRQ(ierr); 516 } 517 ierr = PetscOptionsGetTruth(PETSC_NULL,"-skip_petscrc",&flag,PETSC_NULL);CHKERRQ(ierr); 518 if (!flag) { 519 ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr); 520 /* warning: assumes all processes have a home directory or none, but nothing in between */ 521 if (pfile[0]) { 522 ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr); 523 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,pfile,PETSC_FALSE);CHKERRQ(ierr); 524 } 525 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,".petscrc",PETSC_FALSE);CHKERRQ(ierr); 526 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,"petscrc",PETSC_FALSE);CHKERRQ(ierr); 527 } 528 529 /* insert environmental options */ 530 { 531 char *eoptions = 0; 532 size_t len = 0; 533 if (!rank) { 534 eoptions = (char*)getenv("PETSC_OPTIONS"); 535 ierr = PetscStrlen(eoptions,&len);CHKERRQ(ierr); 536 ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 537 } else { 538 ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 539 if (len) { 540 ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr); 541 } 542 } 543 if (len) { 544 ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 545 if (rank) eoptions[len] = 0; 546 ierr = PetscOptionsInsertString(eoptions);CHKERRQ(ierr); 547 if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);} 548 } 549 } 550 551 /* insert command line options */ 552 if (argc && args && *argc) { 553 int left = *argc - 1; 554 char **eargs = *args + 1; 555 PetscTruth isoptions_file,isp4,tisp4,isp4yourname,isp4rmrank; 556 557 while (left) { 558 ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr); 559 ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr); 560 ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr); 561 ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr); 562 ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr); 563 isp4 = (PetscTruth) (isp4 || tisp4); 564 ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr); 565 isp4 = (PetscTruth) (isp4 || tisp4); 566 ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr); 567 568 if (eargs[0][0] != '-') { 569 eargs++; left--; 570 } else if (isoptions_file) { 571 if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option"); 572 if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option"); 573 ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,eargs[1],PETSC_TRUE);CHKERRQ(ierr); 574 eargs += 2; left -= 2; 575 576 /* 577 These are "bad" options that MPICH, etc put on the command line 578 we strip them out here. 579 */ 580 } else if (tisp4 || isp4rmrank) { 581 eargs += 1; left -= 1; 582 } else if (isp4 || isp4yourname) { 583 eargs += 2; left -= 2; 584 } else if ((left < 2) || ((eargs[1][0] == '-') && 585 ((eargs[1][1] > '9') || (eargs[1][1] < '0')))) { 586 ierr = PetscOptionsSetValue(eargs[0],PETSC_NULL);CHKERRQ(ierr); 587 eargs++; left--; 588 } else { 589 ierr = PetscOptionsSetValue(eargs[0],eargs[1]);CHKERRQ(ierr); 590 eargs += 2; left -= 2; 591 } 592 } 593 } 594 PetscFunctionReturn(0); 595 } 596 597 #undef __FUNCT__ 598 #define __FUNCT__ "PetscOptionsPrint" 599 /*@C 600 PetscOptionsPrint - Prints the options that have been loaded. This is 601 useful for debugging purposes. 602 603 Logically Collective on PETSC_COMM_WORLD 604 605 Input Parameter: 606 . FILE fd - location to print options (usually stdout or stderr) 607 608 Options Database Key: 609 . -optionstable - Activates PetscOptionsPrint() within PetscFinalize() 610 611 Level: advanced 612 613 Concepts: options database^printing 614 615 .seealso: PetscOptionsAllUsed() 616 @*/ 617 PetscErrorCode PETSC_DLLEXPORT PetscOptionsPrint(FILE *fd) 618 { 619 PetscErrorCode ierr; 620 PetscInt i; 621 622 PetscFunctionBegin; 623 if (!fd) fd = PETSC_STDOUT; 624 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 625 if (options->N) { 626 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"#PETSc Option Table entries:\n");CHKERRQ(ierr); 627 } else { 628 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"#No PETSc Option Table entries\n");CHKERRQ(ierr); 629 } 630 for (i=0; i<options->N; i++) { 631 if (options->values[i]) { 632 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr); 633 } else { 634 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"-%s\n",options->names[i]);CHKERRQ(ierr); 635 } 636 } 637 if (options->N) { 638 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"#End of PETSc Option Table entries\n");CHKERRQ(ierr); 639 } 640 PetscFunctionReturn(0); 641 } 642 643 #undef __FUNCT__ 644 #define __FUNCT__ "PetscOptionsGetAll" 645 /*@C 646 PetscOptionsGetAll - Lists all the options the program was run with in a single string. 647 648 Not Collective 649 650 Output Parameter: 651 . copts - pointer where string pointer is stored 652 653 Notes: the array and each entry in the array should be freed with PetscFree() 654 655 Level: advanced 656 657 Concepts: options database^listing 658 659 .seealso: PetscOptionsAllUsed(), PetscOptionsPrint() 660 @*/ 661 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetAll(char *copts[]) 662 { 663 PetscErrorCode ierr; 664 PetscInt i; 665 size_t len = 1,lent; 666 char *coptions; 667 668 PetscFunctionBegin; 669 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 670 671 /* count the length of the required string */ 672 for (i=0; i<options->N; i++) { 673 ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr); 674 len += 2 + lent; 675 if (options->values[i]) { 676 ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr); 677 len += 1 + lent; 678 } 679 } 680 ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr); 681 coptions[0] = 0; 682 for (i=0; i<options->N; i++) { 683 ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr); 684 ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr); 685 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 686 if (options->values[i]) { 687 ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr); 688 ierr = PetscStrcat(coptions," ");CHKERRQ(ierr); 689 } 690 } 691 *copts = coptions; 692 PetscFunctionReturn(0); 693 } 694 695 #undef __FUNCT__ 696 #define __FUNCT__ "PetscOptionsClear" 697 /*@C 698 PetscOptionsClear - Removes all options form the database leaving it empty. 699 700 Level: developer 701 702 .seealso: PetscOptionsInsert() 703 @*/ 704 PetscErrorCode PETSC_DLLEXPORT PetscOptionsClear(void) 705 { 706 PetscInt i; 707 708 PetscFunctionBegin; 709 if (!options) PetscFunctionReturn(0); 710 for (i=0; i<options->N; i++) { 711 if (options->names[i]) free(options->names[i]); 712 if (options->values[i]) free(options->values[i]); 713 } 714 for (i=0; i<options->Naliases; i++) { 715 free(options->aliases1[i]); 716 free(options->aliases2[i]); 717 } 718 options->N = 0; 719 options->Naliases = 0; 720 PetscFunctionReturn(0); 721 } 722 723 #undef __FUNCT__ 724 #define __FUNCT__ "PetscOptionsDestroy" 725 /*@C 726 PetscOptionsDestroy - Destroys the option database. 727 728 Note: 729 Since PetscOptionsDestroy() is called by PetscFinalize(), the user 730 typically does not need to call this routine. 731 732 Level: developer 733 734 .seealso: PetscOptionsInsert() 735 @*/ 736 PetscErrorCode PETSC_DLLEXPORT PetscOptionsDestroy(void) 737 { 738 PetscErrorCode ierr; 739 740 PetscFunctionBegin; 741 if (!options) PetscFunctionReturn(0); 742 ierr = PetscOptionsClear();CHKERRQ(ierr); 743 free(options); 744 options = 0; 745 PetscFunctionReturn(0); 746 } 747 748 #undef __FUNCT__ 749 #define __FUNCT__ "PetscOptionsSetValue" 750 /*@C 751 PetscOptionsSetValue - Sets an option name-value pair in the options 752 database, overriding whatever is already present. 753 754 Not collective, but setting values on certain processors could cause problems 755 for parallel objects looking for options. 756 757 Input Parameters: 758 + name - name of option, this SHOULD have the - prepended 759 - value - the option value (not used for all options) 760 761 Level: intermediate 762 763 Note: 764 Only some options have values associated with them, such as 765 -ksp_rtol tol. Other options stand alone, such as -ksp_monitor. 766 767 Concepts: options database^adding option 768 769 .seealso: PetscOptionsInsert() 770 @*/ 771 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetValue(const char iname[],const char value[]) 772 { 773 size_t len; 774 PetscErrorCode ierr; 775 PetscInt N,n,i; 776 char **names; 777 const char *name = (char*)iname; 778 PetscTruth gt,match; 779 780 PetscFunctionBegin; 781 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 782 783 /* this is so that -h and -hel\p are equivalent (p4 does not like -help)*/ 784 ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr); 785 if (match) name = "-help"; 786 787 name++; 788 /* first check against aliases */ 789 N = options->Naliases; 790 for (i=0; i<N; i++) { 791 ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr); 792 if (match) { 793 name = options->aliases2[i]; 794 break; 795 } 796 } 797 798 N = options->N; 799 n = N; 800 names = options->names; 801 802 for (i=0; i<N; i++) { 803 ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr); 804 ierr = PetscStrgrt(names[i],name,>);CHKERRQ(ierr); 805 if (match) { 806 if (options->values[i]) free(options->values[i]); 807 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 808 if (len) { 809 options->values[i] = (char*)malloc((len+1)*sizeof(char)); 810 ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr); 811 } else { options->values[i] = 0;} 812 PetscOptionsMonitor(name,value); 813 PetscFunctionReturn(0); 814 } else if (gt) { 815 n = i; 816 break; 817 } 818 } 819 if (N >= MAXOPTIONS) { 820 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); 821 } 822 /* shift remaining values down 1 */ 823 for (i=N; i>n; i--) { 824 options->names[i] = options->names[i-1]; 825 options->values[i] = options->values[i-1]; 826 options->used[i] = options->used[i-1]; 827 } 828 /* insert new name and value */ 829 ierr = PetscStrlen(name,&len);CHKERRQ(ierr); 830 options->names[n] = (char*)malloc((len+1)*sizeof(char)); 831 ierr = PetscStrcpy(options->names[n],name);CHKERRQ(ierr); 832 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 833 if (len) { 834 options->values[n] = (char*)malloc((len+1)*sizeof(char)); 835 ierr = PetscStrcpy(options->values[n],value);CHKERRQ(ierr); 836 } else {options->values[n] = 0;} 837 options->used[n] = PETSC_FALSE; 838 options->N++; 839 PetscOptionsMonitor(name,value); 840 PetscFunctionReturn(0); 841 } 842 843 #undef __FUNCT__ 844 #define __FUNCT__ "PetscOptionsClearValue" 845 /*@C 846 PetscOptionsClearValue - Clears an option name-value pair in the options 847 database, overriding whatever is already present. 848 849 Not Collective, but setting values on certain processors could cause problems 850 for parallel objects looking for options. 851 852 Input Parameter: 853 . name - name of option, this SHOULD have the - prepended 854 855 Level: intermediate 856 857 Concepts: options database^removing option 858 .seealso: PetscOptionsInsert() 859 @*/ 860 PetscErrorCode PETSC_DLLEXPORT PetscOptionsClearValue(const char iname[]) 861 { 862 PetscErrorCode ierr; 863 PetscInt N,n,i; 864 char **names,*name=(char*)iname; 865 PetscTruth gt,match; 866 867 PetscFunctionBegin; 868 if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name); 869 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 870 871 name++; 872 873 N = options->N; n = 0; 874 names = options->names; 875 876 for (i=0; i<N; i++) { 877 ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr); 878 ierr = PetscStrgrt(names[i],name,>);CHKERRQ(ierr); 879 if (match) { 880 if (options->names[i]) free(options->names[i]); 881 if (options->values[i]) free(options->values[i]); 882 PetscOptionsMonitor(name,""); 883 break; 884 } else if (gt) { 885 PetscFunctionReturn(0); /* it was not listed */ 886 } 887 n++; 888 } 889 if (n == N) PetscFunctionReturn(0); /* it was not listed */ 890 891 /* shift remaining values down 1 */ 892 for (i=n; i<N-1; i++) { 893 options->names[i] = options->names[i+1]; 894 options->values[i] = options->values[i+1]; 895 options->used[i] = options->used[i+1]; 896 } 897 options->N--; 898 PetscFunctionReturn(0); 899 } 900 901 #undef __FUNCT__ 902 #define __FUNCT__ "PetscOptionsSetAlias" 903 /*@C 904 PetscOptionsSetAlias - Makes a key and alias for another key 905 906 Not Collective, but setting values on certain processors could cause problems 907 for parallel objects looking for options. 908 909 Input Parameters: 910 + inewname - the alias 911 - ioldname - the name that alias will refer to 912 913 Level: advanced 914 915 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 916 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(), 917 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 918 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 919 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 920 PetscOptionsList(), PetscOptionsEList() 921 @*/ 922 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetAlias(const char inewname[],const char ioldname[]) 923 { 924 PetscErrorCode ierr; 925 PetscInt n = options->Naliases; 926 size_t len; 927 char *newname = (char *)inewname,*oldname = (char*)ioldname; 928 929 PetscFunctionBegin; 930 if (newname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname); 931 if (oldname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname); 932 if (n >= MAXALIASES) { 933 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); 934 } 935 936 newname++; oldname++; 937 ierr = PetscStrlen(newname,&len);CHKERRQ(ierr); 938 options->aliases1[n] = (char*)malloc((len+1)*sizeof(char)); 939 ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr); 940 ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr); 941 options->aliases2[n] = (char*)malloc((len+1)*sizeof(char)); 942 ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr); 943 options->Naliases++; 944 PetscFunctionReturn(0); 945 } 946 947 #undef __FUNCT__ 948 #define __FUNCT__ "PetscOptionsFindPair_Private" 949 static PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscTruth *flg) 950 { 951 PetscErrorCode ierr; 952 PetscInt i,N; 953 size_t len; 954 char **names,tmp[256]; 955 PetscTruth match; 956 957 PetscFunctionBegin; 958 if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);} 959 N = options->N; 960 names = options->names; 961 962 if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name); 963 964 /* append prefix to name */ 965 if (pre) { 966 if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -"); 967 ierr = PetscStrncpy(tmp,pre,256);CHKERRQ(ierr); 968 ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr); 969 ierr = PetscStrncat(tmp,name+1,256-len-1);CHKERRQ(ierr); 970 } else { 971 ierr = PetscStrncpy(tmp,name+1,256);CHKERRQ(ierr); 972 } 973 974 /* slow search */ 975 *flg = PETSC_FALSE; 976 for (i=0; i<N; i++) { 977 ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr); 978 if (match) { 979 *value = options->values[i]; 980 options->used[i] = PETSC_TRUE; 981 *flg = PETSC_TRUE; 982 break; 983 } 984 } 985 if (!*flg) { 986 PetscInt j,cnt = 0,locs[16],loce[16]; 987 size_t n; 988 ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr); 989 /* determine the location and number of all _%d_ in the key */ 990 for (i=0; i< (PetscInt)n; i++) { 991 if (tmp[i] == '_') { 992 for (j=i+1; j< (PetscInt)n; j++) { 993 if (tmp[j] >= '0' && tmp[j] <= '9') continue; 994 if (tmp[j] == '_' && j > i+1) { /* found a number */ 995 locs[cnt] = i+1; 996 loce[cnt++] = j+1; 997 } 998 break; 999 } 1000 } 1001 } 1002 if (cnt) { 1003 char tmp2[256]; 1004 for (i=0; i<cnt; i++) { 1005 ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr); 1006 ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr); 1007 ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr); 1008 ierr = PetscOptionsFindPair_Private(PETSC_NULL,tmp2,value,flg);CHKERRQ(ierr); 1009 if (*flg) break; 1010 } 1011 } 1012 } 1013 PetscFunctionReturn(0); 1014 } 1015 1016 #undef __FUNCT__ 1017 #define __FUNCT__ "PetscOptionsReject" 1018 /*@C 1019 PetscOptionsReject - Generates an error if a certain option is given. 1020 1021 Not Collective, but setting values on certain processors could cause problems 1022 for parallel objects looking for options. 1023 1024 Input Parameters: 1025 + name - the option one is seeking 1026 - mess - error message (may be PETSC_NULL) 1027 1028 Level: advanced 1029 1030 Concepts: options database^rejecting option 1031 1032 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(), 1033 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1034 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1035 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1036 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1037 PetscOptionsList(), PetscOptionsEList() 1038 @*/ 1039 PetscErrorCode PETSC_DLLEXPORT PetscOptionsReject(const char name[],const char mess[]) 1040 { 1041 PetscErrorCode ierr; 1042 PetscTruth flag = PETSC_FALSE; 1043 1044 PetscFunctionBegin; 1045 ierr = PetscOptionsHasName(PETSC_NULL,name,&flag);CHKERRQ(ierr); 1046 if (flag) { 1047 if (mess) { 1048 SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess); 1049 } else { 1050 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name); 1051 } 1052 } 1053 PetscFunctionReturn(0); 1054 } 1055 1056 #undef __FUNCT__ 1057 #define __FUNCT__ "PetscOptionsHasName" 1058 /*@C 1059 PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even 1060 its value is set to false. 1061 1062 Not Collective 1063 1064 Input Parameters: 1065 + name - the option one is seeking 1066 - pre - string to prepend to the name or PETSC_NULL 1067 1068 Output Parameters: 1069 . flg - PETSC_TRUE if found else PETSC_FALSE. 1070 1071 Level: beginner 1072 1073 Concepts: options database^has option name 1074 1075 Notes: Name cannot be simply -h 1076 1077 In many cases you probably want to use PetscOptionsGetTruth() instead of calling this, to allowing toggling values. 1078 1079 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1080 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1081 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1082 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1083 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1084 PetscOptionsList(), PetscOptionsEList() 1085 @*/ 1086 PetscErrorCode PETSC_DLLEXPORT PetscOptionsHasName(const char pre[],const char name[],PetscTruth *flg) 1087 { 1088 char *value; 1089 PetscErrorCode ierr; 1090 PetscTruth flag; 1091 1092 PetscFunctionBegin; 1093 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1094 if (flg) *flg = flag; 1095 PetscFunctionReturn(0); 1096 } 1097 1098 #undef __FUNCT__ 1099 #define __FUNCT__ "PetscOptionsGetInt" 1100 /*@C 1101 PetscOptionsGetInt - Gets the integer value for a particular option in the database. 1102 1103 Not Collective 1104 1105 Input Parameters: 1106 + pre - the string to prepend to the name or PETSC_NULL 1107 - name - the option one is seeking 1108 1109 Output Parameter: 1110 + ivalue - the integer value to return 1111 - flg - PETSC_TRUE if found, else PETSC_FALSE 1112 1113 Level: beginner 1114 1115 Concepts: options database^has int 1116 1117 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 1118 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth() 1119 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(), 1120 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1121 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1122 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1123 PetscOptionsList(), PetscOptionsEList() 1124 @*/ 1125 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscTruth *flg) 1126 { 1127 char *value; 1128 PetscErrorCode ierr; 1129 PetscTruth flag; 1130 1131 PetscFunctionBegin; 1132 PetscValidCharPointer(name,2); 1133 PetscValidIntPointer(ivalue,3); 1134 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1135 if (flag) { 1136 if (!value) {if (flg) *flg = PETSC_FALSE;} 1137 else { 1138 if (flg) *flg = PETSC_TRUE; 1139 ierr = PetscOptionsAtoi(value,ivalue);CHKERRQ(ierr); 1140 } 1141 } else { 1142 if (flg) *flg = PETSC_FALSE; 1143 } 1144 PetscFunctionReturn(0); 1145 } 1146 1147 #undef __FUNCT__ 1148 #define __FUNCT__ "PetscOptionsGetEList" 1149 /*@C 1150 PetscOptionsGetEList - Puts a list of option values that a single one may be selected from 1151 1152 Not Collective 1153 1154 Input Parameters: 1155 + pre - the string to prepend to the name or PETSC_NULL 1156 . opt - option name 1157 . list - the possible choices 1158 . ntext - number of choices 1159 1160 Output Parameter: 1161 + value - the index of the value to return 1162 - set - PETSC_TRUE if found, else PETSC_FALSE 1163 1164 Level: intermediate 1165 1166 See PetscOptionsList() for when the choices are given in a PetscFList() 1167 1168 Concepts: options database^list 1169 1170 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1171 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1172 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1173 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1174 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1175 PetscOptionsList(), PetscOptionsEList() 1176 @*/ 1177 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEList(const char pre[],const char opt[],const char *const*list,PetscInt ntext,PetscInt *value,PetscTruth *set) 1178 { 1179 PetscErrorCode ierr; 1180 size_t alen,len = 0; 1181 char *svalue; 1182 PetscTruth aset,flg = PETSC_FALSE; 1183 PetscInt i; 1184 1185 PetscFunctionBegin; 1186 for ( i=0; i<ntext; i++) { 1187 ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr); 1188 if (alen > len) len = alen; 1189 } 1190 len += 5; /* a little extra space for user mistypes */ 1191 ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr); 1192 ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr); 1193 if (aset) { 1194 if (set) *set = PETSC_TRUE; 1195 for (i=0; i<ntext; i++) { 1196 ierr = PetscStrcasecmp(svalue,list[i],&flg);CHKERRQ(ierr); 1197 if (flg) { 1198 *value = i; 1199 break; 1200 } 1201 } 1202 if (!flg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre?pre:"",opt+1); 1203 } else if (set) { 1204 *set = PETSC_FALSE; 1205 } 1206 ierr = PetscFree(svalue);CHKERRQ(ierr); 1207 PetscFunctionReturn(0); 1208 } 1209 1210 #undef __FUNCT__ 1211 #define __FUNCT__ "PetscOptionsEnum" 1212 /*@C 1213 PetscOptionsGetEnum - Gets the enum value for a particular option in the database. 1214 1215 Not Collective 1216 1217 Input Parameters: 1218 + pre - option prefix or PETSC_NULL 1219 . opt - option name 1220 . list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null 1221 - defaultv - the default (current) value 1222 1223 Output Parameter: 1224 + value - the value to return 1225 - flg - PETSC_TRUE if found, else PETSC_FALSE 1226 1227 Level: beginner 1228 1229 Concepts: options database 1230 1231 Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd() 1232 1233 list is usually something like PCASMTypes or some other predefined list of enum names 1234 1235 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(), 1236 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth() 1237 PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(), 1238 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1239 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1240 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1241 PetscOptionsList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum() 1242 @*/ 1243 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEnum(const char pre[],const char opt[],const char *const*list,PetscEnum *value,PetscTruth *set) 1244 { 1245 PetscErrorCode ierr; 1246 PetscInt ntext = 0,tval; 1247 PetscTruth fset; 1248 1249 PetscFunctionBegin; 1250 while (list[ntext++]) { 1251 if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries"); 1252 } 1253 if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix"); 1254 ntext -= 3; 1255 ierr = PetscOptionsGetEList(pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr); 1256 /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */ 1257 if (fset) *value = (PetscEnum)tval; 1258 if (set) *set = fset; 1259 PetscFunctionReturn(0); 1260 } 1261 1262 #undef __FUNCT__ 1263 #define __FUNCT__ "PetscOptionsGetTruth" 1264 /*@C 1265 PetscOptionsGetTruth - Gets the Logical (true or false) value for a particular 1266 option in the database. 1267 1268 Not Collective 1269 1270 Input Parameters: 1271 + pre - the string to prepend to the name or PETSC_NULL 1272 - name - the option one is seeking 1273 1274 Output Parameter: 1275 + ivalue - the logical value to return 1276 - flg - PETSC_TRUE if found, else PETSC_FALSE 1277 1278 Level: beginner 1279 1280 Notes: 1281 TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE 1282 FALSE, false, NO, no, and 0 all translate to PETSC_FALSE 1283 1284 If the user does not supply the option (as either true or false) ivalue is NOT changed. Thus 1285 you NEED TO ALWAYS initialize the ivalue. 1286 1287 Concepts: options database^has logical 1288 1289 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), 1290 PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsTruth(), 1291 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1292 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1293 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1294 PetscOptionsList(), PetscOptionsEList() 1295 @*/ 1296 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruth(const char pre[],const char name[],PetscTruth *ivalue,PetscTruth *flg) 1297 { 1298 char *value; 1299 PetscTruth flag; 1300 PetscErrorCode ierr; 1301 1302 PetscFunctionBegin; 1303 PetscValidCharPointer(name,2); 1304 PetscValidIntPointer(ivalue,3); 1305 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1306 if (flag) { 1307 if (flg) *flg = PETSC_TRUE; 1308 if (!value) { 1309 *ivalue = PETSC_TRUE; 1310 } else { 1311 ierr = PetscOptionsAtol(value, ivalue);CHKERRQ(ierr); 1312 } 1313 } else { 1314 if (flg) *flg = PETSC_FALSE; 1315 } 1316 PetscFunctionReturn(0); 1317 } 1318 1319 #undef __FUNCT__ 1320 #define __FUNCT__ "PetscOptionsGetTruthArray" 1321 /*@C 1322 PetscOptionsGetTruthArray - Gets an array of Logical (true or false) values for a particular 1323 option in the database. The values must be separated with commas with 1324 no intervening spaces. 1325 1326 Not Collective 1327 1328 Input Parameters: 1329 + pre - string to prepend to each name or PETSC_NULL 1330 . name - the option one is seeking 1331 - nmax - maximum number of values to retrieve 1332 1333 Output Parameter: 1334 + dvalue - the integer values to return 1335 . nmax - actual number of values retreived 1336 - flg - PETSC_TRUE if found, else PETSC_FALSE 1337 1338 Level: beginner 1339 1340 Concepts: options database^array of ints 1341 1342 Notes: 1343 TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE 1344 FALSE, false, NO, no, and 0 all translate to PETSC_FALSE 1345 1346 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1347 PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1348 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1349 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1350 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1351 PetscOptionsList(), PetscOptionsEList() 1352 @*/ 1353 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruthArray(const char pre[],const char name[],PetscTruth dvalue[],PetscInt *nmax,PetscTruth *flg) 1354 { 1355 char *value; 1356 PetscErrorCode ierr; 1357 PetscInt n = 0; 1358 PetscTruth flag; 1359 PetscToken token; 1360 1361 PetscFunctionBegin; 1362 PetscValidCharPointer(name,2); 1363 PetscValidIntPointer(dvalue,3); 1364 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1365 if (!flag) {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 1366 if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);} 1367 1368 if (flg) *flg = PETSC_TRUE; 1369 1370 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1371 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1372 while (n < *nmax) { 1373 if (!value) break; 1374 ierr = PetscOptionsAtol(value,dvalue);CHKERRQ(ierr); 1375 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1376 dvalue++; 1377 n++; 1378 } 1379 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1380 *nmax = n; 1381 PetscFunctionReturn(0); 1382 } 1383 1384 #undef __FUNCT__ 1385 #define __FUNCT__ "PetscOptionsGetReal" 1386 /*@C 1387 PetscOptionsGetReal - Gets the double precision value for a particular 1388 option in the database. 1389 1390 Not Collective 1391 1392 Input Parameters: 1393 + pre - string to prepend to each name or PETSC_NULL 1394 - name - the option one is seeking 1395 1396 Output Parameter: 1397 + dvalue - the double value to return 1398 - flg - PETSC_TRUE if found, PETSC_FALSE if not found 1399 1400 Level: beginner 1401 1402 Concepts: options database^has double 1403 1404 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1405 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(), 1406 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1407 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1408 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1409 PetscOptionsList(), PetscOptionsEList() 1410 @*/ 1411 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscTruth *flg) 1412 { 1413 char *value; 1414 PetscErrorCode ierr; 1415 PetscTruth flag; 1416 1417 PetscFunctionBegin; 1418 PetscValidCharPointer(name,2); 1419 PetscValidDoublePointer(dvalue,3); 1420 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1421 if (flag) { 1422 if (!value) {if (flg) *flg = PETSC_FALSE;} 1423 else {if (flg) *flg = PETSC_TRUE; ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);} 1424 } else { 1425 if (flg) *flg = PETSC_FALSE; 1426 } 1427 PetscFunctionReturn(0); 1428 } 1429 1430 #undef __FUNCT__ 1431 #define __FUNCT__ "PetscOptionsGetScalar" 1432 /*@C 1433 PetscOptionsGetScalar - Gets the scalar value for a particular 1434 option in the database. 1435 1436 Not Collective 1437 1438 Input Parameters: 1439 + pre - string to prepend to each name or PETSC_NULL 1440 - name - the option one is seeking 1441 1442 Output Parameter: 1443 + dvalue - the double value to return 1444 - flg - PETSC_TRUE if found, else PETSC_FALSE 1445 1446 Level: beginner 1447 1448 Usage: 1449 A complex number 2+3i can be specified as 2,3 at the command line. 1450 or a number 2.0e-10 - 3.3e-20 i can be specified as 2.0e-10,3.3e-20 1451 1452 Concepts: options database^has scalar 1453 1454 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1455 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1456 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1457 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1458 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1459 PetscOptionsList(), PetscOptionsEList() 1460 @*/ 1461 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscTruth *flg) 1462 { 1463 char *value; 1464 PetscTruth flag; 1465 PetscErrorCode ierr; 1466 1467 PetscFunctionBegin; 1468 PetscValidCharPointer(name,2); 1469 PetscValidScalarPointer(dvalue,3); 1470 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1471 if (flag) { 1472 if (!value) { 1473 if (flg) *flg = PETSC_FALSE; 1474 } else { 1475 #if !defined(PETSC_USE_COMPLEX) 1476 ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr); 1477 #else 1478 PetscReal re=0.0,im=0.0; 1479 PetscToken token; 1480 char *tvalue = 0; 1481 1482 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1483 ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr); 1484 if (!tvalue) { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"unknown string specified\n"); } 1485 ierr = PetscOptionsAtod(tvalue,&re);CHKERRQ(ierr); 1486 ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr); 1487 if (!tvalue) { /* Unknown separator used. using only real value */ 1488 *dvalue = re; 1489 } else { 1490 ierr = PetscOptionsAtod(tvalue,&im);CHKERRQ(ierr); 1491 *dvalue = re + PETSC_i*im; 1492 } 1493 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1494 #endif 1495 if (flg) *flg = PETSC_TRUE; 1496 } 1497 } else { /* flag */ 1498 if (flg) *flg = PETSC_FALSE; 1499 } 1500 PetscFunctionReturn(0); 1501 } 1502 1503 #undef __FUNCT__ 1504 #define __FUNCT__ "PetscOptionsGetRealArray" 1505 /*@C 1506 PetscOptionsGetRealArray - Gets an array of double precision values for a 1507 particular option in the database. The values must be separated with 1508 commas with no intervening spaces. 1509 1510 Not Collective 1511 1512 Input Parameters: 1513 + pre - string to prepend to each name or PETSC_NULL 1514 . name - the option one is seeking 1515 - nmax - maximum number of values to retrieve 1516 1517 Output Parameters: 1518 + dvalue - the double value to return 1519 . nmax - actual number of values retreived 1520 - flg - PETSC_TRUE if found, else PETSC_FALSE 1521 1522 Level: beginner 1523 1524 Concepts: options database^array of doubles 1525 1526 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1527 PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(), 1528 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1529 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1530 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1531 PetscOptionsList(), PetscOptionsEList() 1532 @*/ 1533 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscTruth *flg) 1534 { 1535 char *value; 1536 PetscErrorCode ierr; 1537 PetscInt n = 0; 1538 PetscTruth flag; 1539 PetscToken token; 1540 1541 PetscFunctionBegin; 1542 PetscValidCharPointer(name,2); 1543 PetscValidDoublePointer(dvalue,3); 1544 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1545 if (!flag) {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 1546 if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);} 1547 1548 if (flg) *flg = PETSC_TRUE; 1549 1550 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1551 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1552 while (n < *nmax) { 1553 if (!value) break; 1554 ierr = PetscOptionsAtod(value,dvalue++);CHKERRQ(ierr); 1555 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1556 n++; 1557 } 1558 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1559 *nmax = n; 1560 PetscFunctionReturn(0); 1561 } 1562 1563 #undef __FUNCT__ 1564 #define __FUNCT__ "PetscOptionsGetIntArray" 1565 /*@C 1566 PetscOptionsGetIntArray - Gets an array of integer values for a particular 1567 option in the database. The values must be separated with commas with 1568 no intervening spaces. 1569 1570 Not Collective 1571 1572 Input Parameters: 1573 + pre - string to prepend to each name or PETSC_NULL 1574 . name - the option one is seeking 1575 - nmax - maximum number of values to retrieve, can include d-D to indicate d,d+1,..,D-1 1576 1577 Output Parameter: 1578 + dvalue - the integer values to return 1579 . nmax - actual number of values retreived 1580 - flg - PETSC_TRUE if found, else PETSC_FALSE 1581 1582 Level: beginner 1583 1584 Concepts: options database^array of ints 1585 1586 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(), 1587 PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1588 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1589 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1590 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1591 PetscOptionsList(), PetscOptionsEList() 1592 @*/ 1593 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscTruth *flg) 1594 { 1595 char *value; 1596 PetscErrorCode ierr; 1597 PetscInt n = 0,i,start,end; 1598 size_t len; 1599 PetscTruth flag,foundrange; 1600 PetscToken token; 1601 1602 PetscFunctionBegin; 1603 PetscValidCharPointer(name,2); 1604 PetscValidIntPointer(dvalue,3); 1605 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1606 if (!flag) {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);} 1607 if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);} 1608 1609 if (flg) *flg = PETSC_TRUE; 1610 1611 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1612 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1613 while (n < *nmax) { 1614 if (!value) break; 1615 1616 /* look for form d-D where d and D are integers */ 1617 foundrange = PETSC_FALSE; 1618 ierr = PetscStrlen(value,&len);CHKERRQ(ierr); 1619 if (value[0] == '-') i=2; 1620 else i=1; 1621 for (;i<(int)len; i++) { 1622 if (value[i] == '-') { 1623 if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value); 1624 value[i] = 0; 1625 ierr = PetscOptionsAtoi(value,&start);CHKERRQ(ierr); 1626 ierr = PetscOptionsAtoi(value+i+1,&end);CHKERRQ(ierr); 1627 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); 1628 if (n + end - start - 1 >= *nmax) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry, not enough space in left in array (%D) to contain entire range from %D to %D",n,*nmax-n,start,end); 1629 for (;start<end; start++) { 1630 *dvalue = start; dvalue++;n++; 1631 } 1632 foundrange = PETSC_TRUE; 1633 break; 1634 } 1635 } 1636 if (!foundrange) { 1637 ierr = PetscOptionsAtoi(value,dvalue);CHKERRQ(ierr); 1638 dvalue++; 1639 n++; 1640 } 1641 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1642 } 1643 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1644 *nmax = n; 1645 PetscFunctionReturn(0); 1646 } 1647 1648 #undef __FUNCT__ 1649 #define __FUNCT__ "PetscOptionsGetString" 1650 /*@C 1651 PetscOptionsGetString - Gets the string value for a particular option in 1652 the database. 1653 1654 Not Collective 1655 1656 Input Parameters: 1657 + pre - string to prepend to name or PETSC_NULL 1658 . name - the option one is seeking 1659 - len - maximum string length 1660 1661 Output Parameters: 1662 + string - location to copy string 1663 - flg - PETSC_TRUE if found, else PETSC_FALSE 1664 1665 Level: beginner 1666 1667 Fortran Note: 1668 The Fortran interface is slightly different from the C/C++ 1669 interface (len is not used). Sample usage in Fortran follows 1670 .vb 1671 character *20 string 1672 integer flg, ierr 1673 call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-s',string,flg,ierr) 1674 .ve 1675 1676 Concepts: options database^string 1677 1678 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1679 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1680 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1681 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1682 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1683 PetscOptionsList(), PetscOptionsEList() 1684 @*/ 1685 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscTruth *flg) 1686 { 1687 char *value; 1688 PetscErrorCode ierr; 1689 PetscTruth flag; 1690 1691 PetscFunctionBegin; 1692 PetscValidCharPointer(name,2); 1693 PetscValidCharPointer(string,3); 1694 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1695 if (!flag) { 1696 if (flg) *flg = PETSC_FALSE; 1697 } else { 1698 if (flg) *flg = PETSC_TRUE; 1699 if (value) { 1700 ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr); 1701 } else { 1702 ierr = PetscMemzero(string,len);CHKERRQ(ierr); 1703 } 1704 } 1705 PetscFunctionReturn(0); 1706 } 1707 1708 #undef __FUNCT__ 1709 #define __FUNCT__ "PetscOptionsGetStringArray" 1710 /*@C 1711 PetscOptionsGetStringArray - Gets an array of string values for a particular 1712 option in the database. The values must be separated with commas with 1713 no intervening spaces. 1714 1715 Not Collective 1716 1717 Input Parameters: 1718 + pre - string to prepend to name or PETSC_NULL 1719 . name - the option one is seeking 1720 - nmax - maximum number of strings 1721 1722 Output Parameter: 1723 + strings - location to copy strings 1724 - flg - PETSC_TRUE if found, else PETSC_FALSE 1725 1726 Level: beginner 1727 1728 Notes: 1729 The user should pass in an array of pointers to char, to hold all the 1730 strings returned by this function. 1731 1732 The user is responsible for deallocating the strings that are 1733 returned. The Fortran interface for this routine is not supported. 1734 1735 Contributed by Matthew Knepley. 1736 1737 Concepts: options database^array of strings 1738 1739 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(), 1740 PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(), 1741 PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), 1742 PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(), 1743 PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(), 1744 PetscOptionsList(), PetscOptionsEList() 1745 @*/ 1746 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscTruth *flg) 1747 { 1748 char *value; 1749 PetscErrorCode ierr; 1750 PetscInt n; 1751 PetscTruth flag; 1752 PetscToken token; 1753 1754 PetscFunctionBegin; 1755 PetscValidCharPointer(name,2); 1756 PetscValidPointer(strings,3); 1757 ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr); 1758 if (!flag) {*nmax = 0; if (flg) *flg = PETSC_FALSE; PetscFunctionReturn(0);} 1759 if (!value) {*nmax = 0; if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);} 1760 if (!*nmax) {if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);} 1761 if (flg) *flg = PETSC_TRUE; 1762 1763 ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr); 1764 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1765 n = 0; 1766 while (n < *nmax) { 1767 if (!value) break; 1768 ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr); 1769 ierr = PetscTokenFind(token,&value);CHKERRQ(ierr); 1770 n++; 1771 } 1772 ierr = PetscTokenDestroy(token);CHKERRQ(ierr); 1773 *nmax = n; 1774 PetscFunctionReturn(0); 1775 } 1776 1777 #undef __FUNCT__ 1778 #define __FUNCT__ "PetscOptionsAllUsed" 1779 /*@C 1780 PetscOptionsAllUsed - Returns a count of the number of options in the 1781 database that have never been selected. 1782 1783 Not Collective 1784 1785 Output Parameter: 1786 . N - count of options not used 1787 1788 Level: advanced 1789 1790 .seealso: PetscOptionsPrint() 1791 @*/ 1792 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAllUsed(int *N) 1793 { 1794 PetscInt i,n = 0; 1795 1796 PetscFunctionBegin; 1797 for (i=0; i<options->N; i++) { 1798 if (!options->used[i]) { n++; } 1799 } 1800 *N = n; 1801 PetscFunctionReturn(0); 1802 } 1803 1804 #undef __FUNCT__ 1805 #define __FUNCT__ "PetscOptionsLeft" 1806 /*@ 1807 PetscOptionsLeft - Prints to screen any options that were set and never used. 1808 1809 Not collective 1810 1811 Options Database Key: 1812 . -options_left - Activates OptionsAllUsed() within PetscFinalize() 1813 1814 Level: advanced 1815 1816 .seealso: PetscOptionsAllUsed() 1817 @*/ 1818 PetscErrorCode PETSC_DLLEXPORT PetscOptionsLeft(void) 1819 { 1820 PetscErrorCode ierr; 1821 PetscInt i; 1822 1823 PetscFunctionBegin; 1824 for (i=0; i<options->N; i++) { 1825 if (!options->used[i]) { 1826 if (options->values[i]) { 1827 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr); 1828 } else { 1829 ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s no value \n",options->names[i]);CHKERRQ(ierr); 1830 } 1831 } 1832 } 1833 PetscFunctionReturn(0); 1834 } 1835 1836 1837 #undef __FUNCT__ 1838 #define __FUNCT__ "PetscOptionsCreate" 1839 /* 1840 PetscOptionsCreate - Creates the empty options database. 1841 1842 */ 1843 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCreate(void) 1844 { 1845 PetscErrorCode ierr; 1846 1847 PetscFunctionBegin; 1848 options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable)); 1849 ierr = PetscMemzero(options->used,MAXOPTIONS*sizeof(PetscTruth));CHKERRQ(ierr); 1850 options->namegiven = PETSC_FALSE; 1851 options->N = 0; 1852 options->Naliases = 0; 1853 options->numbermonitors = 0; 1854 1855 PetscOptionsObject.prefix = PETSC_NULL; 1856 PetscOptionsObject.title = PETSC_NULL; 1857 1858 PetscFunctionReturn(0); 1859 } 1860 1861 #undef __FUNCT__ 1862 #define __FUNCT__ "PetscOptionsSetFromOptions" 1863 /*@ 1864 PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options. 1865 1866 Collective on PETSC_COMM_WORLD 1867 1868 Options Database Keys: 1869 + -options_monitor <optional filename> - prints the names and values of all runtime options as they are set. The monitor functionality is not 1870 available for options set through a file, environment variable, or on 1871 the command line. Only options set after PetscInitialize completes will 1872 be monitored. 1873 . -options_monitor_cancel - cancel all options database monitors 1874 1875 Notes: 1876 To see all options, run your program with the -help option or consult 1877 the users manual. 1878 1879 Level: intermediate 1880 1881 .keywords: set, options, database 1882 @*/ 1883 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetFromOptions(void) 1884 { 1885 PetscTruth flgc,flgm; 1886 PetscErrorCode ierr; 1887 char monfilename[PETSC_MAX_PATH_LEN]; 1888 PetscViewer monviewer; 1889 1890 PetscFunctionBegin; 1891 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr); 1892 ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flgm);CHKERRQ(ierr); 1893 ierr = PetscOptionsTruth("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",PETSC_FALSE,&flgc,PETSC_NULL);CHKERRQ(ierr); 1894 ierr = PetscOptionsEnd();CHKERRQ(ierr); 1895 if (flgm) { 1896 ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr); 1897 ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr); 1898 } 1899 if (flgc) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); } 1900 PetscFunctionReturn(0); 1901 } 1902 1903 1904 #undef __FUNCT__ 1905 #define __FUNCT__ "PetscOptionsMonitorDefault" 1906 /*@C 1907 PetscOptionsMonitorDefault - Print all options set value events. 1908 1909 Logically Collective on PETSC_COMM_WORLD 1910 1911 Input Parameters: 1912 + name - option name string 1913 . value - option value string 1914 - dummy - unused monitor context 1915 1916 Level: intermediate 1917 1918 .keywords: PetscOptions, default, monitor 1919 1920 .seealso: PetscOptionsMonitorSet() 1921 @*/ 1922 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy) 1923 { 1924 PetscErrorCode ierr; 1925 PetscViewer viewer = (PetscViewer) dummy; 1926 1927 PetscFunctionBegin; 1928 if (!viewer) { 1929 ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); 1930 } 1931 ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr); 1932 PetscFunctionReturn(0); 1933 } 1934 1935 #undef __FUNCT__ 1936 #define __FUNCT__ "PetscOptionsMonitorSet" 1937 /*@C 1938 PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that 1939 modified the PETSc options database. 1940 1941 Not collective 1942 1943 Input Parameters: 1944 + monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring 1945 . mctx - [optional] context for private data for the 1946 monitor routine (use PETSC_NULL if no context is desired) 1947 - monitordestroy - [optional] routine that frees monitor context 1948 (may be PETSC_NULL) 1949 1950 Calling Sequence of monitor: 1951 $ monitor (const char name[], const char value[], void *mctx) 1952 1953 + name - option name string 1954 . value - option value string 1955 - mctx - optional monitoring context, as set by PetscOptionsMonitorSet() 1956 1957 Options Database Keys: 1958 + -options_monitor - sets PetscOptionsMonitorDefault() 1959 - -options_monitor_cancel - cancels all monitors that have 1960 been hardwired into a code by 1961 calls to PetscOptionsMonitorSet(), but 1962 does not cancel those set via 1963 the options database. 1964 1965 Notes: 1966 The default is to do nothing. To print the name and value of options 1967 being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine, 1968 with a null monitoring context. 1969 1970 Several different monitoring routines may be set by calling 1971 PetscOptionsMonitorSet() multiple times; all will be called in the 1972 order in which they were set. 1973 1974 Level: beginner 1975 1976 .keywords: PetscOptions, set, monitor 1977 1978 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel() 1979 @*/ 1980 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void*)) 1981 { 1982 PetscFunctionBegin; 1983 if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set"); 1984 options->monitor[options->numbermonitors] = monitor; 1985 options->monitordestroy[options->numbermonitors] = monitordestroy; 1986 options->monitorcontext[options->numbermonitors++] = (void*)mctx; 1987 PetscFunctionReturn(0); 1988 } 1989 1990 #undef __FUNCT__ 1991 #define __FUNCT__ "PetscOptionsMonitorCancel" 1992 /*@ 1993 PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object. 1994 1995 Not collective 1996 1997 Options Database Key: 1998 . -options_monitor_cancel - Cancels all monitors that have 1999 been hardwired into a code by calls to PetscOptionsMonitorSet(), 2000 but does not cancel those set via the options database. 2001 2002 Level: intermediate 2003 2004 .keywords: PetscOptions, set, monitor 2005 2006 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet() 2007 @*/ 2008 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorCancel(void) 2009 { 2010 PetscErrorCode ierr; 2011 PetscInt i; 2012 2013 PetscFunctionBegin; 2014 for (i=0; i<options->numbermonitors; i++) { 2015 if (options->monitordestroy[i]) { 2016 ierr = (*options->monitordestroy[i])(options->monitorcontext[i]);CHKERRQ(ierr); 2017 } 2018 } 2019 options->numbermonitors = 0; 2020 PetscFunctionReturn(0); 2021 } 2022