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