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