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