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