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