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