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