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