xref: /petsc/src/sys/objects/options.c (revision 11525c0d8e626c522c766a3408f7e8671b0dac87)
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           PetscOptionsFList(), 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           PetscOptionsFList(), 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     char fullpath[PETSC_MAX_PATH_LEN];
608 
609     ierr = PetscStrreplace(PETSC_COMM_WORLD,file,fullpath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
610     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,fullpath,PETSC_TRUE);CHKERRQ(ierr);
611   }
612   /*
613      We want to be able to give -skip_petscrc on the command line, but need to parse it first.  Since the command line
614      should take precedence, we insert it twice.  It would be sufficient to just scan for -skip_petscrc.
615   */
616   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(*argc,*args);CHKERRQ(ierr);}
617   ierr = PetscOptionsGetBool(NULL,"-skip_petscrc",&flag,NULL);CHKERRQ(ierr);
618   if (!flag) {
619     ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
620     /* PetscOptionsInsertFile() does a fopen() on rank0 only - so only rank0 HomeDir value is relavent */
621     if (pfile[0]) { ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr); }
622     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,pfile,PETSC_FALSE);CHKERRQ(ierr);
623     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,".petscrc",PETSC_FALSE);CHKERRQ(ierr);
624     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,"petscrc",PETSC_FALSE);CHKERRQ(ierr);
625   }
626 
627   /* insert environmental options */
628   {
629     char   *eoptions = 0;
630     size_t len       = 0;
631     if (!rank) {
632       eoptions = (char*)getenv("PETSC_OPTIONS");
633       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
634       ierr     = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
635     } else {
636       ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
637       if (len) {
638         ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr);
639       }
640     }
641     if (len) {
642       ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
643       if (rank) eoptions[len] = 0;
644       ierr = PetscOptionsInsertString(eoptions);CHKERRQ(ierr);
645       if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);}
646     }
647   }
648 
649 #if defined(PETSC_HAVE_YAML)
650   char      yaml_file[PETSC_MAX_PATH_LEN];
651   PetscBool yaml_flg = PETSC_FALSE;
652   ierr = PetscOptionsGetString(NULL,"-options_file_yaml",yaml_file,PETSC_MAX_PATH_LEN,&yaml_flg);CHKERRQ(ierr);
653   if (yaml_flg) ierr = PetscOptionsInsertFileYAML(PETSC_COMM_WORLD,yaml_file,PETSC_TRUE);CHKERRQ(ierr);
654 #endif
655 
656   /* insert command line options again because they take precedence over arguments in petscrc/environment */
657   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs_Private(*argc,*args);CHKERRQ(ierr);}
658   PetscFunctionReturn(0);
659 }
660 
661 #undef __FUNCT__
662 #define __FUNCT__ "PetscOptionsView"
663 /*@C
664    PetscOptionsView - Prints the options that have been loaded. This is
665    useful for debugging purposes.
666 
667    Logically Collective on PetscViewer
668 
669    Input Parameter:
670 .  viewer - must be an PETSCVIEWERASCII viewer
671 
672    Options Database Key:
673 .  -options_table - Activates PetscOptionsView() within PetscFinalize()
674 
675    Level: advanced
676 
677    Concepts: options database^printing
678 
679 .seealso: PetscOptionsAllUsed()
680 @*/
681 PetscErrorCode  PetscOptionsView(PetscViewer viewer)
682 {
683   PetscErrorCode ierr;
684   PetscInt       i;
685   PetscBool      isascii;
686 
687   PetscFunctionBegin;
688   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
689   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
690   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only supports ASCII viewer");
691 
692   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
693   if (options->N) {
694     ierr = PetscViewerASCIIPrintf(viewer,"#PETSc Option Table entries:\n");CHKERRQ(ierr);
695   } else {
696     ierr = PetscViewerASCIIPrintf(viewer,"#No PETSc Option Table entries\n");CHKERRQ(ierr);
697   }
698   for (i=0; i<options->N; i++) {
699     if (options->values[i]) {
700       ierr = PetscViewerASCIIPrintf(viewer,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
701     } else {
702       ierr = PetscViewerASCIIPrintf(viewer,"-%s\n",options->names[i]);CHKERRQ(ierr);
703     }
704   }
705   if (options->N) {
706     ierr = PetscViewerASCIIPrintf(viewer,"#End of PETSc Option Table entries\n");CHKERRQ(ierr);
707   }
708   PetscFunctionReturn(0);
709 }
710 
711 #undef __FUNCT__
712 #define __FUNCT__ "PetscOptionsGetAll"
713 /*@C
714    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
715 
716    Not Collective
717 
718    Output Parameter:
719 .  copts - pointer where string pointer is stored
720 
721    Notes: the array and each entry in the array should be freed with PetscFree()
722 
723    Level: advanced
724 
725    Concepts: options database^listing
726 
727 .seealso: PetscOptionsAllUsed(), PetscOptionsView()
728 @*/
729 PetscErrorCode  PetscOptionsGetAll(char *copts[])
730 {
731   PetscErrorCode ierr;
732   PetscInt       i;
733   size_t         len       = 1,lent = 0;
734   char           *coptions = NULL;
735 
736   PetscFunctionBegin;
737   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
738 
739   /* count the length of the required string */
740   for (i=0; i<options->N; i++) {
741     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
742     len += 2 + lent;
743     if (options->values[i]) {
744       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
745       len += 1 + lent;
746     }
747   }
748   ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr);
749   coptions[0] = 0;
750   for (i=0; i<options->N; i++) {
751     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
752     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
753     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
754     if (options->values[i]) {
755       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
756       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
757     }
758   }
759   *copts = coptions;
760   PetscFunctionReturn(0);
761 }
762 
763 #undef __FUNCT__
764 #define __FUNCT__ "PetscOptionsPrefixPush"
765 /*@
766    PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow.
767 
768    Not Collective, but prefix will only be applied on calling ranks
769 
770    Input Parameter:
771 .  prefix - The string to append to the existing prefix
772 
773    Options Database Keys:
774  +   -prefix_push <some_prefix_> - push the given prefix
775  -   -prefix_pop - pop the last prefix
776 
777    Notes:
778    It is common to use this in conjunction with -options_file as in
779 
780  $ -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop
781 
782    where the files no longer require all options to be prefixed with -system2_.
783 
784 Level: advanced
785 
786 .seealso: PetscOptionsPrefixPop()
787 @*/
788 PetscErrorCode  PetscOptionsPrefixPush(const char prefix[])
789 {
790   PetscErrorCode ierr;
791   size_t         n;
792   PetscInt       start;
793   char           buf[2048];
794   PetscBool      key;
795 
796   PetscFunctionBegin;
797   PetscValidCharPointer(prefix,1);
798   /* Want to check validity of the key using PetscOptionsValidKey(), which requires that the first character is a '-' */
799   buf[0] = '-';
800   ierr = PetscStrncpy(buf+1,prefix,sizeof(buf) - 1);CHKERRQ(ierr);
801   buf[sizeof(buf) - 1] = 0;
802   ierr = PetscOptionsValidKey(buf,&key);CHKERRQ(ierr);
803   if (!key) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Given prefix \"%s\" not valid (the first character must be a letter, do not include leading '-')",prefix);
804 
805   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
806   if (options->prefixind >= MAXPREFIXES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum depth of prefix stack %d exceeded, recompile \n src/sys/objects/options.c with larger value for MAXPREFIXES",MAXPREFIXES);
807   start = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
808   ierr = PetscStrlen(prefix,&n);CHKERRQ(ierr);
809   if (n+1 > sizeof(options->prefix)-start) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum prefix length %d exceeded",sizeof(options->prefix));
810   ierr = PetscMemcpy(options->prefix+start,prefix,n+1);CHKERRQ(ierr);
811   options->prefixstack[options->prefixind++] = start+n;
812   PetscFunctionReturn(0);
813 }
814 
815 #undef __FUNCT__
816 #define __FUNCT__ "PetscOptionsPrefixPop"
817 /*@
818    PetscOptionsPrefixPop - Remove the latest options prefix, see PetscOptionsPrefixPush() for details
819 
820    Not  Collective, but prefix will only be popped on calling ranks
821 
822    Level: advanced
823 
824 .seealso: PetscOptionsPrefixPush()
825 @*/
826 PetscErrorCode  PetscOptionsPrefixPop(void)
827 {
828   PetscInt offset;
829 
830   PetscFunctionBegin;
831   if (options->prefixind < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More prefixes popped than pushed");
832   options->prefixind--;
833   offset = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
834   options->prefix[offset] = 0;
835   PetscFunctionReturn(0);
836 }
837 
838 #undef __FUNCT__
839 #define __FUNCT__ "PetscOptionsClear"
840 /*@C
841     PetscOptionsClear - Removes all options form the database leaving it empty.
842 
843    Level: developer
844 
845 .seealso: PetscOptionsInsert()
846 @*/
847 PetscErrorCode  PetscOptionsClear(void)
848 {
849   PetscInt i;
850 
851   PetscFunctionBegin;
852   if (!options) PetscFunctionReturn(0);
853   for (i=0; i<options->N; i++) {
854     if (options->names[i])  free(options->names[i]);
855     if (options->values[i]) free(options->values[i]);
856   }
857   for (i=0; i<options->Naliases; i++) {
858     free(options->aliases1[i]);
859     free(options->aliases2[i]);
860   }
861   options->prefix[0] = 0;
862   options->prefixind = 0;
863   options->N         = 0;
864   options->Naliases  = 0;
865   PetscFunctionReturn(0);
866 }
867 
868 #undef __FUNCT__
869 #define __FUNCT__ "PetscOptionsDestroy"
870 /*@C
871     PetscOptionsDestroy - Destroys the option database.
872 
873     Note:
874     Since PetscOptionsDestroy() is called by PetscFinalize(), the user
875     typically does not need to call this routine.
876 
877    Level: developer
878 
879 .seealso: PetscOptionsInsert()
880 @*/
881 PetscErrorCode  PetscOptionsDestroy(void)
882 {
883   PetscErrorCode ierr;
884 
885   PetscFunctionBegin;
886   if (!options) PetscFunctionReturn(0);
887   ierr = PetscOptionsClear();CHKERRQ(ierr);
888   free(options);
889   options = 0;
890   PetscFunctionReturn(0);
891 }
892 
893 #undef __FUNCT__
894 #define __FUNCT__ "PetscOptionsSetValue"
895 /*@C
896    PetscOptionsSetValue - Sets an option name-value pair in the options
897    database, overriding whatever is already present.
898 
899    Not collective, but setting values on certain processors could cause problems
900    for parallel objects looking for options.
901 
902    Input Parameters:
903 +  name - name of option, this SHOULD have the - prepended
904 -  value - the option value (not used for all options)
905 
906    Level: intermediate
907 
908    Note:
909    Only some options have values associated with them, such as
910    -ksp_rtol tol.  Other options stand alone, such as -ksp_monitor.
911 
912   Developers Note: Uses malloc() directly because PETSc may not yet have been fully initialized
913 
914   Concepts: options database^adding option
915 
916 .seealso: PetscOptionsInsert()
917 @*/
918 PetscErrorCode  PetscOptionsSetValue(const char iname[],const char value[])
919 {
920   size_t         len;
921   PetscErrorCode ierr;
922   PetscInt       N,n,i;
923   char           **names;
924   char           fullname[2048];
925   const char     *name = iname;
926   PetscBool      gt,match;
927 
928   PetscFunctionBegin;
929   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
930 
931   /* this is so that -h and -hel\p are equivalent (p4 does not like -help)*/
932   ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr);
933   if (match) name = "-help";
934 
935   name++; /* skip starting hyphen */
936   if (options->prefixind > 0) {
937     ierr = PetscStrncpy(fullname,options->prefix,sizeof(fullname));CHKERRQ(ierr);
938     ierr = PetscStrncat(fullname,name,sizeof(fullname));CHKERRQ(ierr);
939     name = fullname;
940   }
941 
942   /* check against aliases */
943   N = options->Naliases;
944   for (i=0; i<N; i++) {
945     ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr);
946     if (match) {
947       name = options->aliases2[i];
948       break;
949     }
950   }
951 
952   N     = options->N;
953   n     = N;
954   names = options->names;
955 
956   for (i=0; i<N; i++) {
957     ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
958     ierr = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
959     if (match) {
960       if (options->values[i]) free(options->values[i]);
961       ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
962       if (len) {
963         options->values[i] = (char*)malloc((len+1)*sizeof(char));
964         ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr);
965       } else options->values[i] = 0;
966       PetscOptionsMonitor(name,value);
967       PetscFunctionReturn(0);
968     } else if (gt) {
969       n = i;
970       break;
971     }
972   }
973   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);
974 
975   /* shift remaining values down 1 */
976   for (i=N; i>n; i--) {
977     options->names[i]  = options->names[i-1];
978     options->values[i] = options->values[i-1];
979     options->used[i]   = options->used[i-1];
980   }
981   /* insert new name and value */
982   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
983   options->names[n] = (char*)malloc((len+1)*sizeof(char));
984   ierr = PetscStrcpy(options->names[n],name);CHKERRQ(ierr);
985   ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
986   if (len) {
987     options->values[n] = (char*)malloc((len+1)*sizeof(char));
988     ierr = PetscStrcpy(options->values[n],value);CHKERRQ(ierr);
989   } else options->values[n] = 0;
990   options->used[n] = PETSC_FALSE;
991   options->N++;
992   PetscOptionsMonitor(name,value);
993   PetscFunctionReturn(0);
994 }
995 
996 #undef __FUNCT__
997 #define __FUNCT__ "PetscOptionsClearValue"
998 /*@C
999    PetscOptionsClearValue - Clears an option name-value pair in the options
1000    database, overriding whatever is already present.
1001 
1002    Not Collective, but setting values on certain processors could cause problems
1003    for parallel objects looking for options.
1004 
1005    Input Parameter:
1006 .  name - name of option, this SHOULD have the - prepended
1007 
1008    Level: intermediate
1009 
1010    Concepts: options database^removing option
1011 .seealso: PetscOptionsInsert()
1012 @*/
1013 PetscErrorCode  PetscOptionsClearValue(const char iname[])
1014 {
1015   PetscErrorCode ierr;
1016   PetscInt       N,n,i;
1017   char           **names,*name=(char*)iname;
1018   PetscBool      gt,match;
1019 
1020   PetscFunctionBegin;
1021   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1022   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
1023 
1024   name++;
1025 
1026   N     = options->N; n = 0;
1027   names = options->names;
1028 
1029   for (i=0; i<N; i++) {
1030     ierr  = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
1031     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
1032     if (match) {
1033       if (options->names[i])  free(options->names[i]);
1034       if (options->values[i]) free(options->values[i]);
1035       PetscOptionsMonitor(name,"");
1036       break;
1037     } else if (gt) PetscFunctionReturn(0); /* it was not listed */
1038 
1039     n++;
1040   }
1041   if (n == N) PetscFunctionReturn(0); /* it was not listed */
1042 
1043   /* shift remaining values down 1 */
1044   for (i=n; i<N-1; i++) {
1045     options->names[i]  = options->names[i+1];
1046     options->values[i] = options->values[i+1];
1047     options->used[i]   = options->used[i+1];
1048   }
1049   options->N--;
1050   PetscFunctionReturn(0);
1051 }
1052 
1053 #undef __FUNCT__
1054 #define __FUNCT__ "PetscOptionsSetAlias"
1055 /*@C
1056    PetscOptionsSetAlias - Makes a key and alias for another key
1057 
1058    Not Collective, but setting values on certain processors could cause problems
1059    for parallel objects looking for options.
1060 
1061    Input Parameters:
1062 +  inewname - the alias
1063 -  ioldname - the name that alias will refer to
1064 
1065    Level: advanced
1066 
1067 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1068            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1069           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1070           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1071           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1072           PetscOptionsFList(), PetscOptionsEList()
1073 @*/
1074 PetscErrorCode  PetscOptionsSetAlias(const char inewname[],const char ioldname[])
1075 {
1076   PetscErrorCode ierr;
1077   PetscInt       n = options->Naliases;
1078   size_t         len;
1079   char           *newname = (char*)inewname,*oldname = (char*)ioldname;
1080 
1081   PetscFunctionBegin;
1082   if (newname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname);
1083   if (oldname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname);
1084   if (n >= MAXALIASES) 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);
1085 
1086   newname++; oldname++;
1087   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
1088   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
1089   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
1090   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
1091   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
1092   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
1093   options->Naliases++;
1094   PetscFunctionReturn(0);
1095 }
1096 
1097 #undef __FUNCT__
1098 #define __FUNCT__ "PetscOptionsFindPair_Private"
1099 PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscBool  *flg)
1100 {
1101   PetscErrorCode ierr;
1102   PetscInt       i,N;
1103   size_t         len;
1104   char           **names,tmp[256];
1105   PetscBool      match;
1106 
1107   PetscFunctionBegin;
1108   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
1109   N     = options->N;
1110   names = options->names;
1111 
1112   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1113 
1114   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1115   if (pre) {
1116     char       *ptr   = tmp;
1117     const char *namep = name;
1118     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
1119     if (name[1] == '-') {
1120       *ptr++ = '-';
1121       namep++;
1122     }
1123     ierr = PetscStrncpy(ptr,pre,tmp+sizeof(tmp)-ptr);CHKERRQ(ierr);
1124     tmp[sizeof(tmp)-1] = 0;
1125     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1126     ierr = PetscStrncat(tmp,namep+1,sizeof(tmp)-len-1);CHKERRQ(ierr);
1127   } else {
1128     ierr = PetscStrncpy(tmp,name+1,sizeof(tmp));CHKERRQ(ierr);
1129     tmp[sizeof(tmp)-1] = 0;
1130   }
1131 #if defined(PETSC_USE_DEBUG)
1132   {
1133     PetscBool valid;
1134     char      key[sizeof(tmp)+1] = "-";
1135 
1136     ierr = PetscMemcpy(key+1,tmp,sizeof(tmp));CHKERRQ(ierr);
1137     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1138     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1139   }
1140 #endif
1141 
1142   /* slow search */
1143   *flg = PETSC_FALSE;
1144   for (i=0; i<N; i++) {
1145     ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr);
1146     if (match) {
1147       *value           = options->values[i];
1148       options->used[i] = PETSC_TRUE;
1149       *flg             = PETSC_TRUE;
1150       break;
1151     }
1152   }
1153   if (!*flg) {
1154     PetscInt j,cnt = 0,locs[16],loce[16];
1155     size_t   n;
1156     ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr);
1157     /* determine the location and number of all _%d_ in the key */
1158     for (i=0; i< (PetscInt)n; i++) {
1159       if (tmp[i] == '_') {
1160         for (j=i+1; j< (PetscInt)n; j++) {
1161           if (tmp[j] >= '0' && tmp[j] <= '9') continue;
1162           if (tmp[j] == '_' && j > i+1) { /* found a number */
1163             locs[cnt]   = i+1;
1164             loce[cnt++] = j+1;
1165           }
1166           break;
1167         }
1168       }
1169     }
1170     if (cnt) {
1171       char tmp2[256];
1172       for (i=0; i<cnt; i++) {
1173         ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr);
1174         ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr);
1175         ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr);
1176         ierr = PetscOptionsFindPair_Private(NULL,tmp2,value,flg);CHKERRQ(ierr);
1177         if (*flg) break;
1178       }
1179     }
1180   }
1181   PetscFunctionReturn(0);
1182 }
1183 
1184 #undef __FUNCT__
1185 #define __FUNCT__ "PetscOptionsFindPairPrefix_Private"
1186 PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(const char pre[], const char name[], char *value[], PetscBool *flg)
1187 {
1188   PetscErrorCode ierr;
1189   PetscInt       i,N;
1190   size_t         len;
1191   char           **names,tmp[256];
1192   PetscBool      match;
1193 
1194   PetscFunctionBegin;
1195   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
1196   N     = options->N;
1197   names = options->names;
1198 
1199   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
1200 
1201   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1202   if (pre) {
1203     char       *ptr   = tmp;
1204     const char *namep = name;
1205     if (pre[0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
1206     if (name[1] == '-') {
1207       *ptr++ = '-';
1208       namep++;
1209     }
1210     ierr = PetscStrncpy(ptr,pre,tmp+sizeof(tmp)-ptr);CHKERRQ(ierr);
1211     tmp[sizeof(tmp)-1] = 0;
1212     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1213     ierr = PetscStrncat(tmp,namep+1,sizeof(tmp)-len-1);CHKERRQ(ierr);
1214   } else {
1215     ierr = PetscStrncpy(tmp,name+1,sizeof(tmp));CHKERRQ(ierr);
1216     tmp[sizeof(tmp)-1] = 0;
1217   }
1218 #if defined(PETSC_USE_DEBUG)
1219   {
1220     PetscBool valid;
1221     char      key[sizeof(tmp)+1] = "-";
1222 
1223     ierr = PetscMemcpy(key+1,tmp,sizeof(tmp));CHKERRQ(ierr);
1224     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1225     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1226   }
1227 #endif
1228 
1229   /* slow search */
1230   *flg = PETSC_FALSE;
1231   ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
1232   for (i = 0; i < N; ++i) {
1233     ierr = PetscStrncmp(names[i], tmp, len, &match);CHKERRQ(ierr);
1234     if (match) {
1235       if (value) *value = options->values[i];
1236       options->used[i]  = PETSC_TRUE;
1237       if (flg)   *flg   = PETSC_TRUE;
1238       break;
1239     }
1240   }
1241   PetscFunctionReturn(0);
1242 }
1243 
1244 #undef __FUNCT__
1245 #define __FUNCT__ "PetscOptionsReject"
1246 /*@C
1247    PetscOptionsReject - Generates an error if a certain option is given.
1248 
1249    Not Collective, but setting values on certain processors could cause problems
1250    for parallel objects looking for options.
1251 
1252    Input Parameters:
1253 +  name - the option one is seeking
1254 -  mess - error message (may be NULL)
1255 
1256    Level: advanced
1257 
1258    Concepts: options database^rejecting option
1259 
1260 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1261            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1262           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1263           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1264           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1265           PetscOptionsFList(), PetscOptionsEList()
1266 @*/
1267 PetscErrorCode  PetscOptionsReject(const char name[],const char mess[])
1268 {
1269   PetscErrorCode ierr;
1270   PetscBool      flag = PETSC_FALSE;
1271 
1272   PetscFunctionBegin;
1273   ierr = PetscOptionsHasName(NULL,name,&flag);CHKERRQ(ierr);
1274   if (flag) {
1275     if (mess) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess);
1276     else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name);
1277   }
1278   PetscFunctionReturn(0);
1279 }
1280 
1281 #undef __FUNCT__
1282 #define __FUNCT__ "PetscOptionsHasName"
1283 /*@C
1284    PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even
1285                       its value is set to false.
1286 
1287    Not Collective
1288 
1289    Input Parameters:
1290 +  name - the option one is seeking
1291 -  pre - string to prepend to the name or NULL
1292 
1293    Output Parameters:
1294 .  set - PETSC_TRUE if found else PETSC_FALSE.
1295 
1296    Level: beginner
1297 
1298    Concepts: options database^has option name
1299 
1300    Notes: Name cannot be simply -h
1301 
1302           In many cases you probably want to use PetscOptionsGetBool() instead of calling this, to allowing toggling values.
1303 
1304 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1305            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1306           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1307           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1308           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1309           PetscOptionsFList(), PetscOptionsEList()
1310 @*/
1311 PetscErrorCode  PetscOptionsHasName(const char pre[],const char name[],PetscBool  *set)
1312 {
1313   char           *value;
1314   PetscErrorCode ierr;
1315   PetscBool      flag;
1316 
1317   PetscFunctionBegin;
1318   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1319   if (set) *set = flag;
1320   PetscFunctionReturn(0);
1321 }
1322 
1323 #undef __FUNCT__
1324 #define __FUNCT__ "PetscOptionsGetInt"
1325 /*@C
1326    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
1327 
1328    Not Collective
1329 
1330    Input Parameters:
1331 +  pre - the string to prepend to the name or NULL
1332 -  name - the option one is seeking
1333 
1334    Output Parameter:
1335 +  ivalue - the integer value to return
1336 -  set - PETSC_TRUE if found, else PETSC_FALSE
1337 
1338    Level: beginner
1339 
1340    Concepts: options database^has int
1341 
1342 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1343           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1344           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1345           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1346           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1347           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1348           PetscOptionsFList(), PetscOptionsEList()
1349 @*/
1350 PetscErrorCode  PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscBool  *set)
1351 {
1352   char           *value;
1353   PetscErrorCode ierr;
1354   PetscBool      flag;
1355 
1356   PetscFunctionBegin;
1357   PetscValidCharPointer(name,2);
1358   PetscValidIntPointer(ivalue,3);
1359   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1360   if (flag) {
1361     if (!value) {
1362       if (set) *set = PETSC_FALSE;
1363     } else {
1364       if (set) *set = PETSC_TRUE;
1365       ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr);
1366     }
1367   } else {
1368     if (set) *set = PETSC_FALSE;
1369   }
1370   PetscFunctionReturn(0);
1371 }
1372 
1373 #undef __FUNCT__
1374 #define __FUNCT__ "PetscOptionsGetEList"
1375 /*@C
1376      PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
1377 
1378    Not Collective
1379 
1380    Input Parameters:
1381 +  pre - the string to prepend to the name or NULL
1382 .  opt - option name
1383 .  list - the possible choices (one of these must be selected, anything else is invalid)
1384 .  ntext - number of choices
1385 
1386    Output Parameter:
1387 +  value - the index of the value to return (defaults to zero if the option name is given but choice is listed)
1388 -  set - PETSC_TRUE if found, else PETSC_FALSE
1389 
1390    Level: intermediate
1391 
1392    See PetscOptionsFList() for when the choices are given in a PetscFunctionList()
1393 
1394    Concepts: options database^list
1395 
1396 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1397            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1398           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1399           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1400           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1401           PetscOptionsFList(), PetscOptionsEList()
1402 @*/
1403 PetscErrorCode  PetscOptionsGetEList(const char pre[],const char opt[],const char * const *list,PetscInt ntext,PetscInt *value,PetscBool  *set)
1404 {
1405   PetscErrorCode ierr;
1406   size_t         alen,len = 0;
1407   char           *svalue;
1408   PetscBool      aset,flg = PETSC_FALSE;
1409   PetscInt       i;
1410 
1411   PetscFunctionBegin;
1412   for (i=0; i<ntext; i++) {
1413     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
1414     if (alen > len) len = alen;
1415   }
1416   len += 5; /* a little extra space for user mistypes */
1417   ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr);
1418   ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr);
1419   if (aset) {
1420     ierr = PetscEListFind(ntext,list,svalue,value,&flg);CHKERRQ(ierr);
1421     if (!flg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre ? pre : "",opt+1);
1422     if (set) *set = PETSC_TRUE;
1423   } else if (set) *set = PETSC_FALSE;
1424   ierr = PetscFree(svalue);CHKERRQ(ierr);
1425   PetscFunctionReturn(0);
1426 }
1427 
1428 #undef __FUNCT__
1429 #define __FUNCT__ "PetscOptionsGetEnum"
1430 /*@C
1431    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
1432 
1433    Not Collective
1434 
1435    Input Parameters:
1436 +  pre - option prefix or NULL
1437 .  opt - option name
1438 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
1439 -  defaultv - the default (current) value
1440 
1441    Output Parameter:
1442 +  value - the  value to return
1443 -  set - PETSC_TRUE if found, else PETSC_FALSE
1444 
1445    Level: beginner
1446 
1447    Concepts: options database
1448 
1449    Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd()
1450 
1451           list is usually something like PCASMTypes or some other predefined list of enum names
1452 
1453 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
1454           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1455           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1456           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1457           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1458           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1459           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
1460 @*/
1461 PetscErrorCode  PetscOptionsGetEnum(const char pre[],const char opt[],const char * const *list,PetscEnum *value,PetscBool  *set)
1462 {
1463   PetscErrorCode ierr;
1464   PetscInt       ntext = 0,tval;
1465   PetscBool      fset;
1466 
1467   PetscFunctionBegin;
1468   while (list[ntext++]) {
1469     if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
1470   }
1471   if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
1472   ntext -= 3;
1473   ierr = PetscOptionsGetEList(pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr);
1474   /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */
1475   if (fset) *value = (PetscEnum)tval;
1476   if (set) *set = fset;
1477   PetscFunctionReturn(0);
1478 }
1479 
1480 #undef __FUNCT__
1481 #define __FUNCT__ "PetscOptionsGetBool"
1482 /*@C
1483    PetscOptionsGetBool - Gets the Logical (true or false) value for a particular
1484             option in the database.
1485 
1486    Not Collective
1487 
1488    Input Parameters:
1489 +  pre - the string to prepend to the name or NULL
1490 -  name - the option one is seeking
1491 
1492    Output Parameter:
1493 +  ivalue - the logical value to return
1494 -  set - PETSC_TRUE  if found, else PETSC_FALSE
1495 
1496    Level: beginner
1497 
1498    Notes:
1499        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1500        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1501 
1502        If the user does not supply the option (as either true or false) ivalue is NOT changed. Thus
1503      you NEED TO ALWAYS initialize the ivalue.
1504 
1505    Concepts: options database^has logical
1506 
1507 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1508           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsBool(),
1509           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1510           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1511           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1512           PetscOptionsFList(), PetscOptionsEList()
1513 @*/
1514 PetscErrorCode  PetscOptionsGetBool(const char pre[],const char name[],PetscBool  *ivalue,PetscBool  *set)
1515 {
1516   char           *value;
1517   PetscBool      flag;
1518   PetscErrorCode ierr;
1519 
1520   PetscFunctionBegin;
1521   PetscValidCharPointer(name,2);
1522   PetscValidIntPointer(ivalue,3);
1523   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1524   if (flag) {
1525     if (set) *set = PETSC_TRUE;
1526     if (!value) *ivalue = PETSC_TRUE;
1527     else {
1528       ierr = PetscOptionsStringToBool(value, ivalue);CHKERRQ(ierr);
1529     }
1530   } else {
1531     if (set) *set = PETSC_FALSE;
1532   }
1533   PetscFunctionReturn(0);
1534 }
1535 
1536 #undef __FUNCT__
1537 #define __FUNCT__ "PetscOptionsGetBoolArray"
1538 /*@C
1539    PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular
1540    option in the database.  The values must be separated with commas with
1541    no intervening spaces.
1542 
1543    Not Collective
1544 
1545    Input Parameters:
1546 +  pre - string to prepend to each name or NULL
1547 .  name - the option one is seeking
1548 -  nmax - maximum number of values to retrieve
1549 
1550    Output Parameter:
1551 +  dvalue - the integer values to return
1552 .  nmax - actual number of values retreived
1553 -  set - PETSC_TRUE if found, else PETSC_FALSE
1554 
1555    Level: beginner
1556 
1557    Concepts: options database^array of ints
1558 
1559    Notes:
1560        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1561        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1562 
1563 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1564            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1565           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1566           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1567           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1568           PetscOptionsFList(), PetscOptionsEList()
1569 @*/
1570 PetscErrorCode  PetscOptionsGetBoolArray(const char pre[],const char name[],PetscBool dvalue[],PetscInt *nmax,PetscBool  *set)
1571 {
1572   char           *value;
1573   PetscErrorCode ierr;
1574   PetscInt       n = 0;
1575   PetscBool      flag;
1576   PetscToken     token;
1577 
1578   PetscFunctionBegin;
1579   PetscValidCharPointer(name,2);
1580   PetscValidIntPointer(dvalue,3);
1581   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1582   if (!flag)  {if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1583   if (!value) {if (set) *set = PETSC_TRUE;  *nmax = 0; PetscFunctionReturn(0);}
1584 
1585   if (set) *set = PETSC_TRUE;
1586 
1587   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1588   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1589   while (n < *nmax) {
1590     if (!value) break;
1591     ierr = PetscOptionsStringToBool(value,dvalue);CHKERRQ(ierr);
1592     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1593     dvalue++;
1594     n++;
1595   }
1596   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
1597   *nmax = n;
1598   PetscFunctionReturn(0);
1599 }
1600 
1601 #undef __FUNCT__
1602 #define __FUNCT__ "PetscOptionsGetReal"
1603 /*@C
1604    PetscOptionsGetReal - Gets the double precision value for a particular
1605    option in the database.
1606 
1607    Not Collective
1608 
1609    Input Parameters:
1610 +  pre - string to prepend to each name or NULL
1611 -  name - the option one is seeking
1612 
1613    Output Parameter:
1614 +  dvalue - the double value to return
1615 -  set - PETSC_TRUE if found, PETSC_FALSE if not found
1616 
1617    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1618 
1619    Level: beginner
1620 
1621    Concepts: options database^has double
1622 
1623 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1624            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
1625           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1626           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1627           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1628           PetscOptionsFList(), PetscOptionsEList()
1629 @*/
1630 PetscErrorCode  PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscBool  *set)
1631 {
1632   char           *value;
1633   PetscErrorCode ierr;
1634   PetscBool      flag;
1635 
1636   PetscFunctionBegin;
1637   PetscValidCharPointer(name,2);
1638   PetscValidRealPointer(dvalue,3);
1639   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1640   if (flag) {
1641     if (!value) {
1642       if (set) *set = PETSC_FALSE;
1643     } else {
1644       if (set) *set = PETSC_TRUE;
1645       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
1646     }
1647   } else {
1648     if (set) *set = PETSC_FALSE;
1649   }
1650   PetscFunctionReturn(0);
1651 }
1652 
1653 #undef __FUNCT__
1654 #define __FUNCT__ "PetscOptionsGetScalar"
1655 /*@C
1656    PetscOptionsGetScalar - Gets the scalar value for a particular
1657    option in the database.
1658 
1659    Not Collective
1660 
1661    Input Parameters:
1662 +  pre - string to prepend to each name or NULL
1663 -  name - the option one is seeking
1664 
1665    Output Parameter:
1666 +  dvalue - the double value to return
1667 -  set - PETSC_TRUE if found, else PETSC_FALSE
1668 
1669    Level: beginner
1670 
1671    Usage:
1672    A complex number 2+3i can be specified as 2,3 at the command line.
1673    or a number 2.0e-10 - 3.3e-20 i  can be specified as 2.0e-10,-3.3e-20
1674 
1675    Note: if the option is given but no value is provided then set is given the value PETSC_FALSE
1676 
1677    Concepts: options database^has scalar
1678 
1679 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1680            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1681           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1682           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1683           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1684           PetscOptionsFList(), PetscOptionsEList()
1685 @*/
1686 PetscErrorCode  PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscBool  *set)
1687 {
1688   char           *value;
1689   PetscBool      flag;
1690   PetscErrorCode ierr;
1691 
1692   PetscFunctionBegin;
1693   PetscValidCharPointer(name,2);
1694   PetscValidScalarPointer(dvalue,3);
1695   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1696   if (flag) {
1697     if (!value) {
1698       if (set) *set = PETSC_FALSE;
1699     } else {
1700 #if !defined(PETSC_USE_COMPLEX)
1701       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
1702 #else
1703       PetscReal  re=0.0,im=0.0;
1704       PetscToken token;
1705       char       *tvalue = 0;
1706 
1707       ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1708       ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1709       if (!tvalue) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"unknown string specified\n");
1710       ierr = PetscOptionsStringToReal(tvalue,&re);CHKERRQ(ierr);
1711       ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1712       if (!tvalue) *dvalue = re; /* Unknown separator used. using only real value */
1713       else {
1714         ierr    = PetscOptionsStringToReal(tvalue,&im);CHKERRQ(ierr);
1715         *dvalue = re + PETSC_i*im;
1716       }
1717       ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
1718 #endif
1719       if (set) *set = PETSC_TRUE;
1720     }
1721   } else { /* flag */
1722     if (set) *set = PETSC_FALSE;
1723   }
1724   PetscFunctionReturn(0);
1725 }
1726 
1727 #undef __FUNCT__
1728 #define __FUNCT__ "PetscOptionsGetRealArray"
1729 /*@C
1730    PetscOptionsGetRealArray - Gets an array of double precision values for a
1731    particular option in the database.  The values must be separated with
1732    commas with no intervening spaces.
1733 
1734    Not Collective
1735 
1736    Input Parameters:
1737 +  pre - string to prepend to each name or NULL
1738 .  name - the option one is seeking
1739 -  nmax - maximum number of values to retrieve
1740 
1741    Output Parameters:
1742 +  dvalue - the double value to return
1743 .  nmax - actual number of values retreived
1744 -  set - PETSC_TRUE if found, else PETSC_FALSE
1745 
1746    Level: beginner
1747 
1748    Concepts: options database^array of doubles
1749 
1750 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1751            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
1752           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1753           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1754           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1755           PetscOptionsFList(), PetscOptionsEList()
1756 @*/
1757 PetscErrorCode  PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscBool  *set)
1758 {
1759   char           *value;
1760   PetscErrorCode ierr;
1761   PetscInt       n = 0;
1762   PetscBool      flag;
1763   PetscToken     token;
1764 
1765   PetscFunctionBegin;
1766   PetscValidCharPointer(name,2);
1767   PetscValidRealPointer(dvalue,3);
1768   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1769   if (!flag) {
1770     if (set) *set = PETSC_FALSE;
1771     *nmax = 0;
1772     PetscFunctionReturn(0);
1773   }
1774   if (!value) {
1775     if (set) *set = PETSC_TRUE;
1776     *nmax = 0;
1777     PetscFunctionReturn(0);
1778   }
1779 
1780   if (set) *set = PETSC_TRUE;
1781 
1782   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1783   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1784   while (n < *nmax) {
1785     if (!value) break;
1786     ierr = PetscOptionsStringToReal(value,dvalue++);CHKERRQ(ierr);
1787     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1788     n++;
1789   }
1790   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
1791   *nmax = n;
1792   PetscFunctionReturn(0);
1793 }
1794 
1795 #undef __FUNCT__
1796 #define __FUNCT__ "PetscOptionsGetIntArray"
1797 /*@C
1798    PetscOptionsGetIntArray - Gets an array of integer values for a particular
1799    option in the database.
1800 
1801    Not Collective
1802 
1803    Input Parameters:
1804 +  pre - string to prepend to each name or NULL
1805 .  name - the option one is seeking
1806 -  nmax - maximum number of values to retrieve
1807 
1808    Output Parameter:
1809 +  dvalue - the integer values to return
1810 .  nmax - actual number of values retreived
1811 -  set - PETSC_TRUE if found, else PETSC_FALSE
1812 
1813    Level: beginner
1814 
1815    Notes:
1816    The array can be passed as
1817    a comma seperated list:                                 0,1,2,3,4,5,6,7
1818    a range (start-end+1):                                  0-8
1819    a range with given increment (start-end+1:inc):         0-7:2
1820    a combination of values and ranges seperated by commas: 0,1-8,8-15:2
1821 
1822    There must be no intervening spaces between the values.
1823 
1824    Concepts: options database^array of ints
1825 
1826 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1827            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1828           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1829           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1830           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1831           PetscOptionsFList(), PetscOptionsEList()
1832 @*/
1833 PetscErrorCode  PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscBool  *set)
1834 {
1835   char           *value;
1836   PetscErrorCode ierr;
1837   PetscInt       n = 0,i,j,start,end,inc,nvalues;
1838   size_t         len;
1839   PetscBool      flag,foundrange;
1840   PetscToken     token;
1841 
1842   PetscFunctionBegin;
1843   PetscValidCharPointer(name,2);
1844   PetscValidIntPointer(dvalue,3);
1845   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1846   if (!flag) {
1847     if (set) *set = PETSC_FALSE;
1848     *nmax = 0;
1849     PetscFunctionReturn(0);
1850   }
1851   if (!value) {
1852     if (set) *set = PETSC_TRUE;
1853     *nmax = 0;
1854     PetscFunctionReturn(0);
1855   }
1856 
1857   if (set) *set = PETSC_TRUE;
1858 
1859   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1860   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1861   while (n < *nmax) {
1862     if (!value) break;
1863 
1864     /* look for form  d-D where d and D are integers */
1865     foundrange = PETSC_FALSE;
1866     ierr       = PetscStrlen(value,&len);CHKERRQ(ierr);
1867     if (value[0] == '-') i=2;
1868     else i=1;
1869     for (;i<(int)len; i++) {
1870       if (value[i] == '-') {
1871         if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
1872         value[i] = 0;
1873 
1874         ierr = PetscOptionsStringToInt(value,&start);CHKERRQ(ierr);
1875         inc  = 1;
1876         j    = i+1;
1877         for (;j<(int)len; j++) {
1878           if (value[j] == ':') {
1879             value[j] = 0;
1880 
1881             ierr = PetscOptionsStringToInt(value+j+1,&inc);CHKERRQ(ierr);
1882             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);
1883             break;
1884           }
1885         }
1886         ierr = PetscOptionsStringToInt(value+i+1,&end);CHKERRQ(ierr);
1887         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);
1888         nvalues = (end-start)/inc + (end-start)%inc;
1889         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);
1890         for (;start<end; start+=inc) {
1891           *dvalue = start; dvalue++;n++;
1892         }
1893         foundrange = PETSC_TRUE;
1894         break;
1895       }
1896     }
1897     if (!foundrange) {
1898       ierr = PetscOptionsStringToInt(value,dvalue);CHKERRQ(ierr);
1899       dvalue++;
1900       n++;
1901     }
1902     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1903   }
1904   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
1905   *nmax = n;
1906   PetscFunctionReturn(0);
1907 }
1908 
1909 #undef __FUNCT__
1910 #define __FUNCT__ "PetscOptionsGetString"
1911 /*@C
1912    PetscOptionsGetString - Gets the string value for a particular option in
1913    the database.
1914 
1915    Not Collective
1916 
1917    Input Parameters:
1918 +  pre - string to prepend to name or NULL
1919 .  name - the option one is seeking
1920 -  len - maximum length of the string including null termination
1921 
1922    Output Parameters:
1923 +  string - location to copy string
1924 -  set - PETSC_TRUE if found, else PETSC_FALSE
1925 
1926    Level: beginner
1927 
1928    Fortran Note:
1929    The Fortran interface is slightly different from the C/C++
1930    interface (len is not used).  Sample usage in Fortran follows
1931 .vb
1932       character *20 string
1933       integer   flg, ierr
1934       call PetscOptionsGetString(NULL_CHARACTER,'-s',string,flg,ierr)
1935 .ve
1936 
1937    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
1938 
1939    Concepts: options database^string
1940 
1941     Note:
1942       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).
1943 
1944 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1945            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1946           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1947           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1948           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1949           PetscOptionsFList(), PetscOptionsEList()
1950 @*/
1951 PetscErrorCode  PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscBool  *set)
1952 {
1953   char           *value;
1954   PetscErrorCode ierr;
1955   PetscBool      flag;
1956 
1957   PetscFunctionBegin;
1958   PetscValidCharPointer(name,2);
1959   PetscValidCharPointer(string,3);
1960   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1961   if (!flag) {
1962     if (set) *set = PETSC_FALSE;
1963   } else {
1964     if (set) *set = PETSC_TRUE;
1965     if (value) {
1966       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
1967       string[len-1] = 0;        /* Ensure that the string is NULL terminated */
1968     } else {
1969       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
1970     }
1971   }
1972   PetscFunctionReturn(0);
1973 }
1974 
1975 #undef __FUNCT__
1976 #define __FUNCT__ "PetscOptionsGetStringMatlab"
1977 char *PetscOptionsGetStringMatlab(const char pre[],const char name[])
1978 {
1979   char           *value;
1980   PetscErrorCode ierr;
1981   PetscBool      flag;
1982 
1983   PetscFunctionBegin;
1984   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);if (ierr) PetscFunctionReturn(0);
1985   if (flag) PetscFunctionReturn(value);
1986   else PetscFunctionReturn(0);
1987 }
1988 
1989 
1990 #undef __FUNCT__
1991 #define __FUNCT__ "PetscOptionsGetStringArray"
1992 /*@C
1993    PetscOptionsGetStringArray - Gets an array of string values for a particular
1994    option in the database. The values must be separated with commas with
1995    no intervening spaces.
1996 
1997    Not Collective
1998 
1999    Input Parameters:
2000 +  pre - string to prepend to name or NULL
2001 .  name - the option one is seeking
2002 -  nmax - maximum number of strings
2003 
2004    Output Parameter:
2005 +  strings - location to copy strings
2006 -  set - PETSC_TRUE if found, else PETSC_FALSE
2007 
2008    Level: beginner
2009 
2010    Notes:
2011    The user should pass in an array of pointers to char, to hold all the
2012    strings returned by this function.
2013 
2014    The user is responsible for deallocating the strings that are
2015    returned. The Fortran interface for this routine is not supported.
2016 
2017    Contributed by Matthew Knepley.
2018 
2019    Concepts: options database^array of strings
2020 
2021 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2022            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2023           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2024           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2025           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2026           PetscOptionsFList(), PetscOptionsEList()
2027 @*/
2028 PetscErrorCode  PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscBool  *set)
2029 {
2030   char           *value;
2031   PetscErrorCode ierr;
2032   PetscInt       n;
2033   PetscBool      flag;
2034   PetscToken     token;
2035 
2036   PetscFunctionBegin;
2037   PetscValidCharPointer(name,2);
2038   PetscValidPointer(strings,3);
2039   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
2040   if (!flag) {
2041     *nmax = 0;
2042     if (set) *set = PETSC_FALSE;
2043     PetscFunctionReturn(0);
2044   }
2045   if (!value) {
2046     *nmax = 0;
2047     if (set) *set = PETSC_FALSE;
2048     PetscFunctionReturn(0);
2049   }
2050   if (!*nmax) {
2051     if (set) *set = PETSC_FALSE;
2052     PetscFunctionReturn(0);
2053   }
2054   if (set) *set = PETSC_TRUE;
2055 
2056   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
2057   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2058   n    = 0;
2059   while (n < *nmax) {
2060     if (!value) break;
2061     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
2062     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2063     n++;
2064   }
2065   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2066   *nmax = n;
2067   PetscFunctionReturn(0);
2068 }
2069 
2070 #undef __FUNCT__
2071 #define __FUNCT__ "PetscOptionsUsed"
2072 /*@C
2073    PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database
2074 
2075    Not Collective
2076 
2077    Input Parameter:
2078 .    option - string name of option
2079 
2080    Output Parameter:
2081 .   used - PETSC_TRUE if the option was used, otherwise false, including if option was not found in options database
2082 
2083    Level: advanced
2084 
2085 .seealso: PetscOptionsView(), PetscOptionsLeft(), PetscOptionsAllUsed()
2086 @*/
2087 PetscErrorCode  PetscOptionsUsed(const char *option,PetscBool *used)
2088 {
2089   PetscInt       i;
2090   PetscErrorCode ierr;
2091 
2092   PetscFunctionBegin;
2093   *used = PETSC_FALSE;
2094   for (i=0; i<options->N; i++) {
2095     ierr = PetscStrcmp(options->names[i],option,used);CHKERRQ(ierr);
2096     if (*used) {
2097       *used = options->used[i];
2098       break;
2099     }
2100   }
2101   PetscFunctionReturn(0);
2102 }
2103 
2104 #undef __FUNCT__
2105 #define __FUNCT__ "PetscOptionsAllUsed"
2106 /*@C
2107    PetscOptionsAllUsed - Returns a count of the number of options in the
2108    database that have never been selected.
2109 
2110    Not Collective
2111 
2112    Output Parameter:
2113 .   N - count of options not used
2114 
2115    Level: advanced
2116 
2117 .seealso: PetscOptionsView()
2118 @*/
2119 PetscErrorCode  PetscOptionsAllUsed(PetscInt *N)
2120 {
2121   PetscInt i,n = 0;
2122 
2123   PetscFunctionBegin;
2124   for (i=0; i<options->N; i++) {
2125     if (!options->used[i]) n++;
2126   }
2127   *N = n;
2128   PetscFunctionReturn(0);
2129 }
2130 
2131 #undef __FUNCT__
2132 #define __FUNCT__ "PetscOptionsLeft"
2133 /*@
2134     PetscOptionsLeft - Prints to screen any options that were set and never used.
2135 
2136   Not collective
2137 
2138    Options Database Key:
2139 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
2140 
2141   Level: advanced
2142 
2143 .seealso: PetscOptionsAllUsed()
2144 @*/
2145 PetscErrorCode  PetscOptionsLeft(void)
2146 {
2147   PetscErrorCode ierr;
2148   PetscInt       i;
2149 
2150   PetscFunctionBegin;
2151   for (i=0; i<options->N; i++) {
2152     if (!options->used[i]) {
2153       if (options->values[i]) {
2154         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
2155       } else {
2156         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s (no value)\n",options->names[i]);CHKERRQ(ierr);
2157       }
2158     }
2159   }
2160   PetscFunctionReturn(0);
2161 }
2162 
2163 
2164 #undef __FUNCT__
2165 #define __FUNCT__ "PetscOptionsCreate"
2166 /*
2167     PetscOptionsCreate - Creates the empty options database.
2168 
2169 */
2170 PetscErrorCode  PetscOptionsCreate(void)
2171 {
2172   PetscErrorCode ierr;
2173 
2174   PetscFunctionBegin;
2175   options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable));
2176   ierr    = PetscMemzero(options,sizeof(PetscOptionsTable));CHKERRQ(ierr);
2177 
2178   options->namegiven      = PETSC_FALSE;
2179   options->N              = 0;
2180   options->Naliases       = 0;
2181   options->numbermonitors = 0;
2182 
2183   PetscOptionsObject.prefix = NULL;
2184   PetscOptionsObject.title  = NULL;
2185   PetscFunctionReturn(0);
2186 }
2187 
2188 #undef __FUNCT__
2189 #define __FUNCT__ "PetscOptionsSetFromOptions"
2190 /*@
2191    PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options.
2192 
2193    Collective on PETSC_COMM_WORLD
2194 
2195    Options Database Keys:
2196 +  -options_monitor <optional filename> - prints the names and values of all runtime options as they are set. The monitor functionality is not
2197                 available for options set through a file, environment variable, or on
2198                 the command line. Only options set after PetscInitialize completes will
2199                 be monitored.
2200 .  -options_monitor_cancel - cancel all options database monitors
2201 
2202    Notes:
2203    To see all options, run your program with the -help option or consult
2204    the <A href="../../docs/manual.pdf">users manual</A>..
2205 
2206    Level: intermediate
2207 
2208 .keywords: set, options, database
2209 @*/
2210 PetscErrorCode  PetscOptionsSetFromOptions(void)
2211 {
2212   PetscBool      flgc,flgm;
2213   PetscErrorCode ierr;
2214   char           monfilename[PETSC_MAX_PATH_LEN];
2215   PetscViewer    monviewer;
2216 
2217   PetscFunctionBegin;
2218   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr);
2219   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flgm);CHKERRQ(ierr);
2220   ierr = PetscOptionsBool("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",PETSC_FALSE,&flgc,NULL);CHKERRQ(ierr);
2221   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2222   if (flgm) {
2223     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
2224     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
2225   }
2226   if (flgc) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
2227   PetscFunctionReturn(0);
2228 }
2229 
2230 
2231 #undef __FUNCT__
2232 #define __FUNCT__ "PetscOptionsMonitorDefault"
2233 /*@C
2234    PetscOptionsMonitorDefault - Print all options set value events.
2235 
2236    Logically Collective on PETSC_COMM_WORLD
2237 
2238    Input Parameters:
2239 +  name  - option name string
2240 .  value - option value string
2241 -  dummy - unused monitor context
2242 
2243    Level: intermediate
2244 
2245 .keywords: PetscOptions, default, monitor
2246 
2247 .seealso: PetscOptionsMonitorSet()
2248 @*/
2249 PetscErrorCode  PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
2250 {
2251   PetscErrorCode ierr;
2252   PetscViewer    viewer = (PetscViewer) dummy;
2253 
2254   PetscFunctionBegin;
2255   if (!viewer) {
2256     ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
2257   }
2258   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
2259   PetscFunctionReturn(0);
2260 }
2261 
2262 #undef __FUNCT__
2263 #define __FUNCT__ "PetscOptionsMonitorSet"
2264 /*@C
2265    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
2266    modified the PETSc options database.
2267 
2268    Not collective
2269 
2270    Input Parameters:
2271 +  monitor - pointer to function (if this is NULL, it turns off monitoring
2272 .  mctx    - [optional] context for private data for the
2273              monitor routine (use NULL if no context is desired)
2274 -  monitordestroy - [optional] routine that frees monitor context
2275           (may be NULL)
2276 
2277    Calling Sequence of monitor:
2278 $     monitor (const char name[], const char value[], void *mctx)
2279 
2280 +  name - option name string
2281 .  value - option value string
2282 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
2283 
2284    Options Database Keys:
2285 +    -options_monitor    - sets PetscOptionsMonitorDefault()
2286 -    -options_monitor_cancel - cancels all monitors that have
2287                           been hardwired into a code by
2288                           calls to PetscOptionsMonitorSet(), but
2289                           does not cancel those set via
2290                           the options database.
2291 
2292    Notes:
2293    The default is to do nothing.  To print the name and value of options
2294    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
2295    with a null monitoring context.
2296 
2297    Several different monitoring routines may be set by calling
2298    PetscOptionsMonitorSet() multiple times; all will be called in the
2299    order in which they were set.
2300 
2301    Level: beginner
2302 
2303 .keywords: PetscOptions, set, monitor
2304 
2305 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
2306 @*/
2307 PetscErrorCode  PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
2308 {
2309   PetscFunctionBegin;
2310   if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
2311   options->monitor[options->numbermonitors]          = monitor;
2312   options->monitordestroy[options->numbermonitors]   = monitordestroy;
2313   options->monitorcontext[options->numbermonitors++] = (void*)mctx;
2314   PetscFunctionReturn(0);
2315 }
2316 
2317 #undef __FUNCT__
2318 #define __FUNCT__ "PetscOptionsMonitorCancel"
2319 /*@
2320    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
2321 
2322    Not collective
2323 
2324    Options Database Key:
2325 .  -options_monitor_cancel - Cancels all monitors that have
2326     been hardwired into a code by calls to PetscOptionsMonitorSet(),
2327     but does not cancel those set via the options database.
2328 
2329    Level: intermediate
2330 
2331 .keywords: PetscOptions, set, monitor
2332 
2333 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
2334 @*/
2335 PetscErrorCode  PetscOptionsMonitorCancel(void)
2336 {
2337   PetscErrorCode ierr;
2338   PetscInt       i;
2339 
2340   PetscFunctionBegin;
2341   for (i=0; i<options->numbermonitors; i++) {
2342     if (options->monitordestroy[i]) {
2343       ierr = (*options->monitordestroy[i])(&options->monitorcontext[i]);CHKERRQ(ierr);
2344     }
2345   }
2346   options->numbermonitors = 0;
2347   PetscFunctionReturn(0);
2348 }
2349