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