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