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