xref: /petsc/src/sys/objects/options.c (revision 7c4f633dc6bb6149cca88d301ead35a99e103cbb)
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;
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 = PetscOptionsHasName(PETSC_NULL,"-skip_petscrc",&flag);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     names[i]           = 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   names[n] = (char*)malloc((len+1)*sizeof(char));
777   ierr = PetscStrcpy(names[n],name);CHKERRQ(ierr);
778   if (value) {
779     ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
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->values[i]) free(options->values[i]);
827       PetscOptionsMonitor(name,"");
828       break;
829     } else if (gt) {
830       PetscFunctionReturn(0); /* it was not listed */
831     }
832     n++;
833   }
834   if (n == N) PetscFunctionReturn(0); /* it was not listed */
835 
836   /* shift remaining values down 1 */
837   for (i=n; i<N-1; i++) {
838     names[i]           = names[i+1];
839     options->values[i] = options->values[i+1];
840     options->used[i]   = options->used[i+1];
841   }
842   options->N--;
843   PetscFunctionReturn(0);
844 }
845 
846 #undef __FUNCT__
847 #define __FUNCT__ "PetscOptionsSetAlias"
848 /*@C
849    PetscOptionsReject - Generates an error if a certain option is given.
850 
851    Not Collective, but setting values on certain processors could cause problems
852    for parallel objects looking for options.
853 
854    Input Parameters:
855 +  name - the option one is seeking
856 -  mess - error message (may be PETSC_NULL)
857 
858    Level: advanced
859 
860    Concepts: options database^rejecting option
861 
862 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
863            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(),
864           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
865           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
866           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
867           PetscOptionsList(), PetscOptionsEList()
868 @*/
869 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetAlias(const char inewname[],const char ioldname[])
870 {
871   PetscErrorCode ierr;
872   PetscInt       n = options->Naliases;
873   size_t         len;
874   char           *newname = (char *)inewname,*oldname = (char*)ioldname;
875 
876   PetscFunctionBegin;
877   if (newname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliased must have -: Instead %s",newname);
878   if (oldname[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"aliasee must have -: Instead %s",oldname);
879   if (n >= MAXALIASES) {
880     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);
881   }
882 
883   newname++; oldname++;
884   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
885   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
886   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
887   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
888   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
889   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
890   options->Naliases++;
891   PetscFunctionReturn(0);
892 }
893 
894 #undef __FUNCT__
895 #define __FUNCT__ "PetscOptionsFindPair_Private"
896 static PetscErrorCode PetscOptionsFindPair_Private(const char pre[],const char name[],char *value[],PetscTruth *flg)
897 {
898   PetscErrorCode ierr;
899   PetscInt       i,N;
900   size_t         len;
901   char           **names,tmp[256];
902   PetscTruth     match;
903 
904   PetscFunctionBegin;
905   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
906   N = options->N;
907   names = options->names;
908 
909   if (name[0] != '-') SETERRQ1(PETSC_ERR_ARG_WRONG,"Name must begin with -: Instead %s",name);
910 
911   /* append prefix to name */
912   if (pre) {
913     if (pre[0] == '-') SETERRQ(PETSC_ERR_ARG_WRONG,"Prefix should not begin with a -");
914     ierr = PetscStrncpy(tmp,pre,256);CHKERRQ(ierr);
915     ierr = PetscStrlen(tmp,&len);CHKERRQ(ierr);
916     ierr = PetscStrncat(tmp,name+1,256-len-1);CHKERRQ(ierr);
917   } else {
918     ierr = PetscStrncpy(tmp,name+1,256);CHKERRQ(ierr);
919   }
920 
921   /* slow search */
922   *flg = PETSC_FALSE;
923   for (i=0; i<N; i++) {
924     ierr = PetscStrcasecmp(names[i],tmp,&match);CHKERRQ(ierr);
925     if (match) {
926        *value           = options->values[i];
927        options->used[i] = PETSC_TRUE;
928        *flg             = PETSC_TRUE;
929        break;
930      }
931   }
932   if (!*flg) {
933     PetscInt j,cnt = 0,locs[16],loce[16];
934     size_t   n;
935     ierr = PetscStrlen(tmp,&n);CHKERRQ(ierr);
936     /* determine the location and number of all _%d_ in the key */
937     for (i=0; i< (PetscInt)n; i++) {
938       if (tmp[i] == '_') {
939         for (j=i+1; j< (PetscInt)n; j++) {
940           if (tmp[j] >= '0' && tmp[j] <= '9') continue;
941           if (tmp[j] == '_' && j > i+1) { /* found a number */
942             locs[cnt]   = i+1;
943             loce[cnt++] = j+1;
944           }
945           break;
946         }
947       }
948     }
949     if (cnt) {
950       char tmp2[256];
951       for (i=0; i<cnt; i++) {
952         ierr = PetscStrcpy(tmp2,"-");CHKERRQ(ierr);
953         ierr = PetscStrncat(tmp2,tmp,locs[i]);CHKERRQ(ierr);
954         ierr = PetscStrcat(tmp2,tmp+loce[i]);CHKERRQ(ierr);
955         ierr = PetscOptionsFindPair_Private(PETSC_NULL,tmp2,value,flg);CHKERRQ(ierr);
956         if (*flg) break;
957       }
958     }
959   }
960   PetscFunctionReturn(0);
961 }
962 
963 #undef __FUNCT__
964 #define __FUNCT__ "PetscOptionsReject"
965 /*@C
966    PetscOptionsReject - Generates an error if a certain option is given.
967 
968    Not Collective, but setting values on certain processors could cause problems
969    for parallel objects looking for options.
970 
971    Input Parameters:
972 +  name - the option one is seeking
973 -  mess - error message (may be PETSC_NULL)
974 
975    Level: advanced
976 
977    Concepts: options database^rejecting option
978 
979 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
980            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
981           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
982           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
983           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
984           PetscOptionsList(), PetscOptionsEList()
985 @*/
986 PetscErrorCode PETSC_DLLEXPORT PetscOptionsReject(const char name[],const char mess[])
987 {
988   PetscErrorCode ierr;
989   PetscTruth     flag;
990 
991   PetscFunctionBegin;
992   ierr = PetscOptionsHasName(PETSC_NULL,name,&flag);CHKERRQ(ierr);
993   if (flag) {
994     if (mess) {
995       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s with %s",name,mess);
996     } else {
997       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: %s",name);
998     }
999   }
1000   PetscFunctionReturn(0);
1001 }
1002 
1003 #undef __FUNCT__
1004 #define __FUNCT__ "PetscOptionsHasName"
1005 /*@C
1006    PetscOptionsHasName - Determines whether a certain option is given in the database.
1007 
1008    Not Collective
1009 
1010    Input Parameters:
1011 +  name - the option one is seeking
1012 -  pre - string to prepend to the name or PETSC_NULL
1013 
1014    Output Parameters:
1015 .  flg - PETSC_TRUE if found else PETSC_FALSE.
1016 
1017    Level: beginner
1018 
1019    Concepts: options database^has option name
1020 
1021    Notes: Name cannot be simply -h
1022 
1023 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1024            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1025           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1026           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1027           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1028           PetscOptionsList(), PetscOptionsEList()
1029 @*/
1030 PetscErrorCode PETSC_DLLEXPORT PetscOptionsHasName(const char pre[],const char name[],PetscTruth *flg)
1031 {
1032   char           *value;
1033   PetscErrorCode ierr;
1034   PetscTruth     isfalse,flag;
1035 
1036   PetscFunctionBegin;
1037   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1038 
1039   /* remove if turned off */
1040   if (flag) {
1041     ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
1042     if (isfalse) flag = PETSC_FALSE;
1043     ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
1044     if (isfalse) flag = PETSC_FALSE;
1045     ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
1046     if (isfalse) flag = PETSC_FALSE;
1047   }
1048   if (flg) *flg = flag;
1049   PetscFunctionReturn(0);
1050 }
1051 
1052 #undef __FUNCT__
1053 #define __FUNCT__ "PetscOptionsGetInt"
1054 /*@C
1055    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
1056 
1057    Not Collective
1058 
1059    Input Parameters:
1060 +  pre - the string to prepend to the name or PETSC_NULL
1061 -  name - the option one is seeking
1062 
1063    Output Parameter:
1064 +  ivalue - the integer value to return
1065 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1066 
1067    Level: beginner
1068 
1069    Concepts: options database^has int
1070 
1071 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1072           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth()
1073           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(),
1074           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1075           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1076           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1077           PetscOptionsList(), PetscOptionsEList()
1078 @*/
1079 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetInt(const char pre[],const char name[],PetscInt *ivalue,PetscTruth *flg)
1080 {
1081   char           *value;
1082   PetscErrorCode ierr;
1083   PetscTruth     flag;
1084 
1085   PetscFunctionBegin;
1086   PetscValidCharPointer(name,2);
1087   PetscValidIntPointer(ivalue,3);
1088   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1089   if (flag) {
1090     if (!value) {if (flg) *flg = PETSC_FALSE;}
1091     else {
1092       if (flg) *flg = PETSC_TRUE;
1093       ierr = PetscOptionsAtoi(value,ivalue);CHKERRQ(ierr);
1094     }
1095   } else {
1096     if (flg) *flg = PETSC_FALSE;
1097   }
1098   PetscFunctionReturn(0);
1099 }
1100 
1101 #undef __FUNCT__
1102 #define __FUNCT__ "PetscOptionsGetEList"
1103 /*@C
1104      PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
1105 
1106    Not Collective
1107 
1108    Input Parameters:
1109 +  pre - the string to prepend to the name or PETSC_NULL
1110 .  opt - option name
1111 .  list - the possible choices
1112 .  ntext - number of choices
1113 
1114    Output Parameter:
1115 +  value - the index of the value to return
1116 -  set - PETSC_TRUE if found, else PETSC_FALSE
1117 
1118    Level: intermediate
1119 
1120    See PetscOptionsList() for when the choices are given in a PetscFList()
1121 
1122    Concepts: options database^list
1123 
1124 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1125            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1126           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1127           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1128           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1129           PetscOptionsList(), PetscOptionsEList()
1130 @*/
1131 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEList(const char pre[],const char opt[],const char **list,PetscInt ntext,PetscInt *value,PetscTruth *set)
1132 {
1133   PetscErrorCode ierr;
1134   size_t         alen,len = 0;
1135   char           *svalue;
1136   PetscTruth     aset,flg = PETSC_FALSE;
1137   PetscInt       i;
1138 
1139   PetscFunctionBegin;
1140   for ( i=0; i<ntext; i++) {
1141     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
1142     if (alen > len) len = alen;
1143   }
1144   len += 5; /* a little extra space for user mistypes */
1145   ierr = PetscMalloc(len*sizeof(char),&svalue);CHKERRQ(ierr);
1146   ierr = PetscOptionsGetString(pre,opt,svalue,len,&aset);CHKERRQ(ierr);
1147   if (aset) {
1148     if (set) *set = PETSC_TRUE;
1149     for (i=0; i<ntext; i++) {
1150       ierr = PetscStrcasecmp(svalue,list[i],&flg);CHKERRQ(ierr);
1151       if (flg) {
1152         *value = i;
1153         break;
1154       }
1155     }
1156     if (!flg) SETERRQ3(PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre?pre:"",opt+1);
1157   } else if (set) {
1158     *set = PETSC_FALSE;
1159   }
1160   ierr = PetscFree(svalue);CHKERRQ(ierr);
1161   PetscFunctionReturn(0);
1162 }
1163 
1164 #undef __FUNCT__
1165 #define __FUNCT__ "PetscOptionsEnum"
1166 /*@C
1167    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
1168 
1169    Not Collective
1170 
1171    Input Parameters:
1172 +  pre - option prefix or PETSC_NULL
1173 .  opt - option name
1174 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
1175 -  defaultv - the default (current) value
1176 
1177    Output Parameter:
1178 +  value - the  value to return
1179 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1180 
1181    Level: beginner
1182 
1183    Concepts: options database
1184 
1185    Notes: Must be between a PetscOptionsBegin() and a PetscOptionsEnd()
1186 
1187           list is usually something like PCASMTypes or some other predefined list of enum names
1188 
1189 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
1190           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth()
1191           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsTruth(),
1192           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1193           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1194           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1195           PetscOptionsList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
1196 @*/
1197 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetEnum(const char pre[],const char opt[],const char **list,PetscEnum *value,PetscTruth *set)
1198 {
1199   PetscErrorCode ierr;
1200   PetscInt       ntext = 0;
1201 
1202   PetscFunctionBegin;
1203   while (list[ntext++]) {
1204     if (ntext > 50) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
1205   }
1206   if (ntext < 3) SETERRQ(PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
1207   ntext -= 3;
1208   ierr = PetscOptionsGetEList(pre,opt,list,ntext,(PetscInt*)value,set);CHKERRQ(ierr);
1209   PetscFunctionReturn(0);
1210 }
1211 
1212 #undef __FUNCT__
1213 #define __FUNCT__ "PetscOptionsGetTruth"
1214 /*@C
1215    PetscOptionsGetTruth - Gets the Logical (true or false) value for a particular
1216             option in the database.
1217 
1218    Not Collective
1219 
1220    Input Parameters:
1221 +  pre - the string to prepend to the name or PETSC_NULL
1222 -  name - the option one is seeking
1223 
1224    Output Parameter:
1225 +  ivalue - the logical value to return
1226 -  flg - PETSC_TRUE  if found, else PETSC_FALSE
1227 
1228    Level: beginner
1229 
1230    Notes:
1231        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1232        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1233 
1234        If the user does not supply the option (as either true or false) ivalue is NOT changed. Thus
1235      you NEED TO ALWAYS initialize the ivalue.
1236 
1237    Concepts: options database^has logical
1238 
1239 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1240           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsTruth(),
1241           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1242           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1243           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1244           PetscOptionsList(), PetscOptionsEList()
1245 @*/
1246 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruth(const char pre[],const char name[],PetscTruth *ivalue,PetscTruth *flg)
1247 {
1248   char           *value;
1249   PetscTruth     flag;
1250   PetscErrorCode ierr;
1251 
1252   PetscFunctionBegin;
1253   PetscValidCharPointer(name,2);
1254   PetscValidIntPointer(ivalue,3);
1255   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1256   if (flag) {
1257     if (flg) *flg = PETSC_TRUE;
1258     if (!value) {
1259       *ivalue = PETSC_TRUE;
1260     } else {
1261       ierr = PetscOptionsAtol(value, ivalue);CHKERRQ(ierr);
1262     }
1263   } else {
1264     if (flg) *flg = PETSC_FALSE;
1265   }
1266   PetscFunctionReturn(0);
1267 }
1268 
1269 #undef __FUNCT__
1270 #define __FUNCT__ "PetscOptionsGetTruthArray"
1271 /*@C
1272    PetscOptionsGetTruthArray - Gets an array of Logical (true or false) values for a particular
1273    option in the database.  The values must be separated with commas with
1274    no intervening spaces.
1275 
1276    Not Collective
1277 
1278    Input Parameters:
1279 +  pre - string to prepend to each name or PETSC_NULL
1280 .  name - the option one is seeking
1281 -  nmax - maximum number of values to retrieve
1282 
1283    Output Parameter:
1284 +  dvalue - the integer values to return
1285 .  nmax - actual number of values retreived
1286 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1287 
1288    Level: beginner
1289 
1290    Concepts: options database^array of ints
1291 
1292    Notes:
1293        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1294        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1295 
1296 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1297            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1298           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1299           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1300           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1301           PetscOptionsList(), PetscOptionsEList()
1302 @*/
1303 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetTruthArray(const char pre[],const char name[],PetscTruth dvalue[],PetscInt *nmax,PetscTruth *flg)
1304 {
1305   char           *value;
1306   PetscErrorCode ierr;
1307   PetscInt       n = 0;
1308   PetscTruth     flag;
1309   PetscToken     token;
1310 
1311   PetscFunctionBegin;
1312   PetscValidCharPointer(name,2);
1313   PetscValidIntPointer(dvalue,3);
1314   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1315   if (!flag)  {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1316   if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1317 
1318   if (flg) *flg = PETSC_TRUE;
1319 
1320   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1321   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1322   while (n < *nmax) {
1323     if (!value) break;
1324     ierr = PetscOptionsAtol(value,dvalue);CHKERRQ(ierr);
1325     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1326     dvalue++;
1327     n++;
1328   }
1329   ierr  = PetscTokenDestroy(token);CHKERRQ(ierr);
1330   *nmax = n;
1331   PetscFunctionReturn(0);
1332 }
1333 
1334 #undef __FUNCT__
1335 #define __FUNCT__ "PetscOptionsGetReal"
1336 /*@C
1337    PetscOptionsGetReal - Gets the double precision value for a particular
1338    option in the database.
1339 
1340    Not Collective
1341 
1342    Input Parameters:
1343 +  pre - string to prepend to each name or PETSC_NULL
1344 -  name - the option one is seeking
1345 
1346    Output Parameter:
1347 +  dvalue - the double value to return
1348 -  flg - PETSC_TRUE if found, PETSC_FALSE if not found
1349 
1350    Level: beginner
1351 
1352    Concepts: options database^has double
1353 
1354 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1355            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsTruth(),
1356           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1357           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1358           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1359           PetscOptionsList(), PetscOptionsEList()
1360 @*/
1361 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetReal(const char pre[],const char name[],PetscReal *dvalue,PetscTruth *flg)
1362 {
1363   char           *value;
1364   PetscErrorCode ierr;
1365   PetscTruth     flag;
1366 
1367   PetscFunctionBegin;
1368   PetscValidCharPointer(name,2);
1369   PetscValidDoublePointer(dvalue,3);
1370   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1371   if (flag) {
1372     if (!value) {if (flg) *flg = PETSC_FALSE;}
1373     else        {if (flg) *flg = PETSC_TRUE; ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);}
1374   } else {
1375     if (flg) *flg = PETSC_FALSE;
1376   }
1377   PetscFunctionReturn(0);
1378 }
1379 
1380 #undef __FUNCT__
1381 #define __FUNCT__ "PetscOptionsGetScalar"
1382 /*@C
1383    PetscOptionsGetScalar - Gets the scalar value for a particular
1384    option in the database.
1385 
1386    Not Collective
1387 
1388    Input Parameters:
1389 +  pre - string to prepend to each name or PETSC_NULL
1390 -  name - the option one is seeking
1391 
1392    Output Parameter:
1393 +  dvalue - the double value to return
1394 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1395 
1396    Level: beginner
1397 
1398    Usage:
1399    A complex number 2+3i can be specified as 2,3 at the command line.
1400    or a number 2.0e-10 - 3.3e-20 i  can be specified as 2.0e-10,3.3e-20
1401 
1402    Concepts: options database^has scalar
1403 
1404 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1405            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1406           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1407           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1408           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1409           PetscOptionsList(), PetscOptionsEList()
1410 @*/
1411 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetScalar(const char pre[],const char name[],PetscScalar *dvalue,PetscTruth *flg)
1412 {
1413   char           *value;
1414   PetscTruth     flag;
1415   PetscErrorCode ierr;
1416 
1417   PetscFunctionBegin;
1418   PetscValidCharPointer(name,2);
1419   PetscValidScalarPointer(dvalue,3);
1420   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1421   if (flag) {
1422     if (!value) {
1423       if (flg) *flg = PETSC_FALSE;
1424     } else {
1425 #if !defined(PETSC_USE_COMPLEX)
1426       ierr = PetscOptionsAtod(value,dvalue);CHKERRQ(ierr);
1427 #else
1428       PetscReal  re=0.0,im=0.0;
1429       PetscToken token;
1430       char       *tvalue = 0;
1431 
1432       ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1433       ierr = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1434       if (!tvalue) { SETERRQ(PETSC_ERR_ARG_WRONG,"unknown string specified\n"); }
1435       ierr    = PetscOptionsAtod(tvalue,&re);CHKERRQ(ierr);
1436       ierr    = PetscTokenFind(token,&tvalue);CHKERRQ(ierr);
1437       if (!tvalue) { /* Unknown separator used. using only real value */
1438         *dvalue = re;
1439       } else {
1440         ierr    = PetscOptionsAtod(tvalue,&im);CHKERRQ(ierr);
1441         *dvalue = re + PETSC_i*im;
1442       }
1443       ierr    = PetscTokenDestroy(token);CHKERRQ(ierr);
1444 #endif
1445       if (flg) *flg    = PETSC_TRUE;
1446     }
1447   } else { /* flag */
1448     if (flg) *flg = PETSC_FALSE;
1449   }
1450   PetscFunctionReturn(0);
1451 }
1452 
1453 #undef __FUNCT__
1454 #define __FUNCT__ "PetscOptionsGetRealArray"
1455 /*@C
1456    PetscOptionsGetRealArray - Gets an array of double precision values for a
1457    particular option in the database.  The values must be separated with
1458    commas with no intervening spaces.
1459 
1460    Not Collective
1461 
1462    Input Parameters:
1463 +  pre - string to prepend to each name or PETSC_NULL
1464 .  name - the option one is seeking
1465 -  nmax - maximum number of values to retrieve
1466 
1467    Output Parameters:
1468 +  dvalue - the double value to return
1469 .  nmax - actual number of values retreived
1470 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1471 
1472    Level: beginner
1473 
1474    Concepts: options database^array of doubles
1475 
1476 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1477            PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
1478           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1479           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1480           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1481           PetscOptionsList(), PetscOptionsEList()
1482 @*/
1483 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetRealArray(const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscTruth *flg)
1484 {
1485   char           *value;
1486   PetscErrorCode ierr;
1487   PetscInt       n = 0;
1488   PetscTruth     flag;
1489   PetscToken     token;
1490 
1491   PetscFunctionBegin;
1492   PetscValidCharPointer(name,2);
1493   PetscValidDoublePointer(dvalue,3);
1494   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1495   if (!flag)  {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1496   if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1497 
1498   if (flg) *flg = PETSC_TRUE;
1499 
1500   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1501   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1502   while (n < *nmax) {
1503     if (!value) break;
1504     ierr = PetscOptionsAtod(value,dvalue++);CHKERRQ(ierr);
1505     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1506     n++;
1507   }
1508   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
1509   *nmax = n;
1510   PetscFunctionReturn(0);
1511 }
1512 
1513 #undef __FUNCT__
1514 #define __FUNCT__ "PetscOptionsGetIntArray"
1515 /*@C
1516    PetscOptionsGetIntArray - Gets an array of integer values for a particular
1517    option in the database.  The values must be separated with commas with
1518    no intervening spaces.
1519 
1520    Not Collective
1521 
1522    Input Parameters:
1523 +  pre - string to prepend to each name or PETSC_NULL
1524 .  name - the option one is seeking
1525 -  nmax - maximum number of values to retrieve
1526 
1527    Output Parameter:
1528 +  dvalue - the integer values to return
1529 .  nmax - actual number of values retreived
1530 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1531 
1532    Level: beginner
1533 
1534    Concepts: options database^array of ints
1535 
1536 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
1537            PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1538           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1539           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1540           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1541           PetscOptionsList(), PetscOptionsEList()
1542 @*/
1543 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetIntArray(const char pre[],const char name[],PetscInt dvalue[],PetscInt *nmax,PetscTruth *flg)
1544 {
1545   char           *value;
1546   PetscErrorCode ierr;
1547   PetscInt       n = 0,i,start,end;
1548   size_t         len;
1549   PetscTruth     flag,foundrange;
1550   PetscToken     token;
1551 
1552   PetscFunctionBegin;
1553   PetscValidCharPointer(name,2);
1554   PetscValidIntPointer(dvalue,3);
1555   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1556   if (!flag)  {if (flg) *flg = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
1557   if (!value) {if (flg) *flg = PETSC_TRUE; *nmax = 0; PetscFunctionReturn(0);}
1558 
1559   if (flg) *flg = PETSC_TRUE;
1560 
1561   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1562   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1563   while (n < *nmax) {
1564     if (!value) break;
1565 
1566     /* look for form  d-D where d and D are integers */
1567     foundrange = PETSC_FALSE;
1568     ierr      = PetscStrlen(value,&len);CHKERRQ(ierr);
1569     if (value[0] == '-') i=2;
1570     else i=1;
1571     for (;i<(int)len; i++) {
1572       if (value[i] == '-') {
1573         if (i == (int)len-1) SETERRQ2(PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
1574         value[i] = 0;
1575         ierr     = PetscOptionsAtoi(value,&start);CHKERRQ(ierr);
1576         ierr     = PetscOptionsAtoi(value+i+1,&end);CHKERRQ(ierr);
1577         if (end <= start) SETERRQ3(PETSC_ERR_USER,"Error in %D-th array entry, %s-%s cannot have decreasing list",n,value,value+i+1);
1578         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);
1579         for (;start<end; start++) {
1580           *dvalue = start; dvalue++;n++;
1581         }
1582         foundrange = PETSC_TRUE;
1583         break;
1584       }
1585     }
1586     if (!foundrange) {
1587       ierr      = PetscOptionsAtoi(value,dvalue);CHKERRQ(ierr);
1588       dvalue++;
1589       n++;
1590     }
1591     ierr      = PetscTokenFind(token,&value);CHKERRQ(ierr);
1592   }
1593   ierr      = PetscTokenDestroy(token);CHKERRQ(ierr);
1594   *nmax = n;
1595   PetscFunctionReturn(0);
1596 }
1597 
1598 #undef __FUNCT__
1599 #define __FUNCT__ "PetscOptionsGetString"
1600 /*@C
1601    PetscOptionsGetString - Gets the string value for a particular option in
1602    the database.
1603 
1604    Not Collective
1605 
1606    Input Parameters:
1607 +  pre - string to prepend to name or PETSC_NULL
1608 .  name - the option one is seeking
1609 -  len - maximum string length
1610 
1611    Output Parameters:
1612 +  string - location to copy string
1613 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1614 
1615    Level: beginner
1616 
1617    Fortran Note:
1618    The Fortran interface is slightly different from the C/C++
1619    interface (len is not used).  Sample usage in Fortran follows
1620 .vb
1621       character *20 string
1622       integer   flg, ierr
1623       call PetscOptionsGetString(PETSC_NULL_CHARACTER,'-s',string,flg,ierr)
1624 .ve
1625 
1626    Concepts: options database^string
1627 
1628 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1629            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1630           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1631           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1632           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1633           PetscOptionsList(), PetscOptionsEList()
1634 @*/
1635 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetString(const char pre[],const char name[],char string[],size_t len,PetscTruth *flg)
1636 {
1637   char           *value;
1638   PetscErrorCode ierr;
1639   PetscTruth     flag;
1640 
1641   PetscFunctionBegin;
1642   PetscValidCharPointer(name,2);
1643   PetscValidCharPointer(string,3);
1644   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1645   if (!flag) {
1646     if (flg) *flg = PETSC_FALSE;
1647   } else {
1648     if (flg) *flg = PETSC_TRUE;
1649     if (value) {
1650       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
1651     } else {
1652       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
1653     }
1654   }
1655   PetscFunctionReturn(0);
1656 }
1657 
1658 #undef __FUNCT__
1659 #define __FUNCT__ "PetscOptionsGetStringArray"
1660 /*@C
1661    PetscOptionsGetStringArray - Gets an array of string values for a particular
1662    option in the database. The values must be separated with commas with
1663    no intervening spaces.
1664 
1665    Not Collective
1666 
1667    Input Parameters:
1668 +  pre - string to prepend to name or PETSC_NULL
1669 .  name - the option one is seeking
1670 -  nmax - maximum number of strings
1671 
1672    Output Parameter:
1673 +  strings - location to copy strings
1674 -  flg - PETSC_TRUE if found, else PETSC_FALSE
1675 
1676    Level: beginner
1677 
1678    Notes:
1679    The user should pass in an array of pointers to char, to hold all the
1680    strings returned by this function.
1681 
1682    The user is responsible for deallocating the strings that are
1683    returned. The Fortran interface for this routine is not supported.
1684 
1685    Contributed by Matthew Knepley.
1686 
1687    Concepts: options database^array of strings
1688 
1689 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1690            PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsTruth(),
1691           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1692           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1693           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
1694           PetscOptionsList(), PetscOptionsEList()
1695 @*/
1696 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetStringArray(const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscTruth *flg)
1697 {
1698   char           *value;
1699   PetscErrorCode ierr;
1700   PetscInt       n;
1701   PetscTruth     flag;
1702   PetscToken     token;
1703 
1704   PetscFunctionBegin;
1705   PetscValidCharPointer(name,2);
1706   PetscValidPointer(strings,3);
1707   ierr = PetscOptionsFindPair_Private(pre,name,&value,&flag);CHKERRQ(ierr);
1708   if (!flag)  {*nmax = 0; if (flg) *flg = PETSC_FALSE; PetscFunctionReturn(0);}
1709   if (!value) {*nmax = 0; if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);}
1710   if (!*nmax) {if (flg) *flg = PETSC_FALSE;PetscFunctionReturn(0);}
1711   if (flg) *flg = PETSC_TRUE;
1712 
1713   ierr = PetscTokenCreate(value,',',&token);CHKERRQ(ierr);
1714   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1715   n = 0;
1716   while (n < *nmax) {
1717     if (!value) break;
1718     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
1719     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
1720     n++;
1721   }
1722   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
1723   *nmax = n;
1724   PetscFunctionReturn(0);
1725 }
1726 
1727 #undef __FUNCT__
1728 #define __FUNCT__ "PetscOptionsAllUsed"
1729 /*@C
1730    PetscOptionsAllUsed - Returns a count of the number of options in the
1731    database that have never been selected.
1732 
1733    Not Collective
1734 
1735    Output Parameter:
1736 .   N - count of options not used
1737 
1738    Level: advanced
1739 
1740 .seealso: PetscOptionsPrint()
1741 @*/
1742 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAllUsed(int *N)
1743 {
1744   PetscInt i,n = 0;
1745 
1746   PetscFunctionBegin;
1747   for (i=0; i<options->N; i++) {
1748     if (!options->used[i]) { n++; }
1749   }
1750   *N = n;
1751   PetscFunctionReturn(0);
1752 }
1753 
1754 #undef __FUNCT__
1755 #define __FUNCT__ "PetscOptionsLeft"
1756 /*@
1757     PetscOptionsLeft - Prints to screen any options that were set and never used.
1758 
1759   Not collective
1760 
1761    Options Database Key:
1762 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
1763 
1764   Level: advanced
1765 
1766 .seealso: PetscOptionsAllUsed()
1767 @*/
1768 PetscErrorCode PETSC_DLLEXPORT PetscOptionsLeft(void)
1769 {
1770   PetscErrorCode ierr;
1771   PetscInt       i;
1772 
1773   PetscFunctionBegin;
1774   for (i=0; i<options->N; i++) {
1775     if (!options->used[i]) {
1776       if (options->values[i]) {
1777         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
1778       } else {
1779         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s no value \n",options->names[i]);CHKERRQ(ierr);
1780       }
1781     }
1782   }
1783   PetscFunctionReturn(0);
1784 }
1785 
1786 
1787 /*
1788     PetscOptionsCreate - Creates the empty options database.
1789 
1790 */
1791 #undef __FUNCT__
1792 #define __FUNCT__ "PetscOptionsCreate"
1793 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCreate(void)
1794 {
1795   PetscErrorCode ierr;
1796 
1797   PetscFunctionBegin;
1798   options = (PetscOptionsTable*)malloc(sizeof(PetscOptionsTable));
1799   ierr    = PetscMemzero(options->used,MAXOPTIONS*sizeof(PetscTruth));CHKERRQ(ierr);
1800   options->namegiven 		= PETSC_FALSE;
1801   options->N         		= 0;
1802   options->Naliases  		= 0;
1803   options->numbermonitors 	= 0;
1804 
1805   PetscOptionsObject.prefix = PETSC_NULL;
1806   PetscOptionsObject.title  = PETSC_NULL;
1807 
1808   PetscFunctionReturn(0);
1809 }
1810 
1811 #undef __FUNCT__
1812 #define __FUNCT__ "PetscOptionsSetFromOptions"
1813 /*@
1814    PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options.
1815 
1816    Collective on PETSC_COMM_WORLD
1817 
1818    Options Database Keys:
1819 +  -options_monitor <optional filename> - prints the names and values of all
1820  				runtime options as they are set. The monitor functionality is not
1821                 available for options set through a file, environment variable, or on
1822                 the command line. Only options set after PetscInitialize completes will
1823                 be monitored.
1824 .  -options_monitor_cancel - cancel all options database monitors
1825 
1826    Notes:
1827    To see all options, run your program with the -help option or consult
1828    the users manual.
1829 
1830    Level: intermediate
1831 
1832 .keywords: set, options, database
1833 @*/
1834 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetFromOptions(void)
1835 {
1836   PetscTruth          flg;
1837   PetscErrorCode      ierr;
1838   char                monfilename[PETSC_MAX_PATH_LEN];
1839   PetscViewer         monviewer;
1840 
1841   PetscFunctionBegin;
1842 
1843   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr);
1844   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1845   if (flg && (!options->numbermonitors)) {
1846     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
1847     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
1848   }
1849 
1850   ierr = PetscOptionsName("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",&flg);CHKERRQ(ierr);
1851   if (flg) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
1852 
1853   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1854 
1855   PetscFunctionReturn(0);
1856 }
1857 
1858 
1859 #undef __FUNCT__
1860 #define __FUNCT__ "PetscOptionsMonitorDefault"
1861 /*@C
1862    PetscOptionsMonitorDefault - Print all options set value events.
1863 
1864    Collective on PETSC_COMM_WORLD
1865 
1866    Input Parameters:
1867 +  name  - option name string
1868 .  value - option value string
1869 -  dummy - unused monitor context
1870 
1871    Level: intermediate
1872 
1873 .keywords: PetscOptions, default, monitor
1874 
1875 .seealso: PetscOptionsMonitorSet()
1876 @*/
1877 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
1878 {
1879   PetscErrorCode ierr;
1880   PetscViewer    viewer = (PetscViewer) dummy;
1881 
1882   PetscFunctionBegin;
1883   if (!viewer) {
1884     ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1885   }
1886   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
1887   PetscFunctionReturn(0);
1888 }
1889 
1890 #undef __FUNCT__
1891 #define __FUNCT__ "PetscOptionsMonitorSet"
1892 /*@C
1893    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
1894    modified the PETSc options database.
1895 
1896    Not collective
1897 
1898    Input Parameters:
1899 +  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring
1900 .  mctx    - [optional] context for private data for the
1901              monitor routine (use PETSC_NULL if no context is desired)
1902 -  monitordestroy - [optional] routine that frees monitor context
1903           (may be PETSC_NULL)
1904 
1905    Calling Sequence of monitor:
1906 $     monitor (const char name[], const char value[], void *mctx)
1907 
1908 +  name - option name string
1909 .  value - option value string
1910 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
1911 
1912    Options Database Keys:
1913 +    -options_monitor    - sets PetscOptionsMonitorDefault()
1914 -    -options_monitor_cancel - cancels all monitors that have
1915                           been hardwired into a code by
1916                           calls to PetscOptionsMonitorSet(), but
1917                           does not cancel those set via
1918                           the options database.
1919 
1920    Notes:
1921    The default is to do nothing.  To print the name and value of options
1922    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
1923    with a null monitoring context.
1924 
1925    Several different monitoring routines may be set by calling
1926    PetscOptionsMonitorSet() multiple times; all will be called in the
1927    order in which they were set.
1928 
1929    Level: beginner
1930 
1931 .keywords: PetscOptions, set, monitor
1932 
1933 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
1934 @*/
1935 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
1936 {
1937   PetscFunctionBegin;
1938   if (options->numbermonitors >= MAXOPTIONSMONITORS) {
1939     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
1940   }
1941   options->monitor[options->numbermonitors]           = monitor;
1942   options->monitordestroy[options->numbermonitors]    = monitordestroy;
1943   options->monitorcontext[options->numbermonitors++]  = (void*)mctx;
1944   PetscFunctionReturn(0);
1945 }
1946 
1947 #undef __FUNCT__
1948 #define __FUNCT__ "PetscOptionsMonitorCancel"
1949 /*@
1950    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
1951 
1952    Not collective
1953 
1954    Options Database Key:
1955 .  -options_monitor_cancel - Cancels all monitors that have
1956     been hardwired into a code by calls to PetscOptionsMonitorSet(),
1957     but does not cancel those set via the options database.
1958 
1959    Level: intermediate
1960 
1961 .keywords: PetscOptions, set, monitor
1962 
1963 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
1964 @*/
1965 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorCancel(void)
1966 {
1967   PetscErrorCode ierr;
1968   PetscInt       i;
1969 
1970   PetscFunctionBegin;
1971   for (i=0; i<options->numbermonitors; i++) {
1972     if (options->monitordestroy[i]) {
1973       ierr = (*options->monitordestroy[i])(options->monitorcontext[i]);CHKERRQ(ierr);
1974     }
1975   }
1976   options->numbermonitors = 0;
1977   PetscFunctionReturn(0);
1978 }
1979