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