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