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