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