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