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