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