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