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