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