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