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