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