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