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