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