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