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