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