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