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