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