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