xref: /petsc/src/sys/objects/options.c (revision b4205f0bfd1bf02f15d622fe83644fa14694706b)
1 /* Define Feature test macros to make sure atoll is available (SVr4, POSIX.1-2001, 4.3BSD, C99), not in (C89 and POSIX.1-1996) */
2 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for atoll() */
3 
4 /*
5    These routines simplify the use of command line, file options, etc., and are used to manipulate the options database.
6    This provides the low-level interface, the high level interface is in aoptions.c
7 
8    Some routines use regular malloc and free because it cannot know  what malloc is requested with the
9    options database until it has already processed the input.
10 */
11 
12 #include <petsc/private/petscimpl.h>        /*I  "petscsys.h"   I*/
13 #include <petscviewer.h>
14 #include <ctype.h>
15 #if defined(PETSC_HAVE_MALLOC_H)
16 #include <malloc.h>
17 #endif
18 #if defined(PETSC_HAVE_STRING_H)
19 #include <string.h>             /* strcasecmp */
20 #endif
21 #if defined(PETSC_HAVE_STRINGS_H)
22 #  include <strings.h>          /* strcasecmp */
23 #endif
24 #if defined(PETSC_HAVE_YAML)
25 #include <yaml.h>
26 #endif
27 
28 #if defined(PETSC_HAVE_STRCASECMP)
29 #define PetscOptNameCmp(a,b) strcasecmp(a,b)
30 #elif defined(PETSC_HAVE_STRICMP)
31 #define PetscOptNameCmp(a,b) stricmp(a,b)
32 #else
33 #define PetscOptNameCmp(a,b) Error_strcasecmp_not_found
34 #endif
35 
36 #include <petsc/private/hashtable.h>
37 
38 /* This assumes ASCII encoding and ignores locale settings */
39 /* Using tolower() is about 2X slower in microbenchmarks   */
40 PETSC_STATIC_INLINE int PetscToLower(int c)
41 {
42   return ((c >= 'A') & (c <= 'Z')) ? c + 'a' - 'A' : c;
43 }
44 
45 /* Bob Jenkins's one at a time hash function (case-insensitive) */
46 PETSC_STATIC_INLINE unsigned int PetscOptHash(const char key[])
47 {
48   unsigned int hash = 0;
49   while (*key) {
50     hash += PetscToLower(*key++);
51     hash += hash << 10;
52     hash ^= hash >>  6;
53   }
54   hash += hash <<  3;
55   hash ^= hash >> 11;
56   hash += hash << 15;
57   return hash;
58 }
59 
60 PETSC_STATIC_INLINE int PetscOptEqual(const char a[],const char b[])
61 {
62   return !PetscOptNameCmp(a,b);
63 }
64 
65 KHASH_INIT(HO, kh_cstr_t, int, 1, PetscOptHash, PetscOptEqual)
66 
67 /*
68     This table holds all the options set by the user. For simplicity, we use a static size database
69 */
70 #define MAXOPTNAME 512
71 #define MAXOPTIONS 512
72 #define MAXALIASES  25
73 #define MAXPREFIXES 25
74 #define MAXOPTIONSMONITORS 5
75 
76 struct  _n_PetscOptions {
77   PetscOptions   next,previous;
78   int            N;                    /* number of options */
79   char           *names[MAXOPTIONS];   /* option names */
80   char           *values[MAXOPTIONS];  /* option values */
81   PetscBool      used[MAXOPTIONS];     /* flag option use */
82 
83   /* Hash table */
84   khash_t(HO)    *ht;
85 
86   /* Prefixes */
87   int            prefixind;
88   int            prefixstack[MAXPREFIXES];
89   char           prefix[MAXOPTNAME];
90 
91   /* Aliases */
92   int            Naliases;                   /* number or aliases */
93   char           *aliases1[MAXALIASES];      /* aliased */
94   char           *aliases2[MAXALIASES];      /* aliasee */
95 
96   /* Help */
97   PetscBool      help; /* flag whether "-help" is in the database */
98 
99   /* Monitors */
100   PetscErrorCode (*monitor[MAXOPTIONSMONITORS])(const char[],const char[],void*); /* returns control to user after */
101   PetscErrorCode (*monitordestroy[MAXOPTIONSMONITORS])(void**);         /* */
102   void           *monitorcontext[MAXOPTIONSMONITORS];                  /* to pass arbitrary user data into monitor */
103   PetscInt       numbermonitors;                                       /* to, for instance, detect options being set */
104 };
105 
106 static PetscOptions defaultoptions = NULL;  /* the options database routines query this object for options */
107 static PetscOptions initialoptions = NULL;  /* this contains the options set by PetscInitialize() */
108 
109 /*
110     Options events monitor
111 */
112 static PetscErrorCode PetscOptionsMonitor(PetscOptions options,const char name[],const char value[])
113 {
114   PetscInt       i;
115   PetscErrorCode ierr;
116 
117   if (!PetscInitializeCalled) return 0;
118   PetscFunctionBegin;
119   for (i=0; i<options->numbermonitors; i++) {
120     ierr = (*options->monitor[i])(name,value,options->monitorcontext[i]);CHKERRQ(ierr);
121   }
122   PetscFunctionReturn(0);
123 }
124 
125 /*@
126    PetscOptionsCreate - Creates an empty options database.
127 
128    Output Parameter:
129 .  options - Options database object
130 
131    Level: advanced
132 
133 .seealso: PetscOptionsDestroy(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsInsert(), PetscOptionsSetValue()
134 @*/
135 PetscErrorCode PetscOptionsCreate(PetscOptions *options)
136 {
137   if (!options) return PETSC_ERR_ARG_NULL;
138   *options = (PetscOptions)calloc(1,sizeof(**options));
139   if (!*options) return PETSC_ERR_MEM;
140   return 0;
141 }
142 
143 /*@
144     PetscOptionsDestroy - Destroys an option database.
145 
146   Input Parameter:
147 .  options - the PetscOptions object
148 
149    Level: developer
150 
151 .seealso: PetscOptionsInsert(), PetscOptionsPush(), PetscOptionsPop(), PetscOptionsInsert(), PetscOptionsSetValue()
152 @*/
153 PetscErrorCode PetscOptionsDestroy(PetscOptions *options)
154 {
155   PetscErrorCode ierr;
156 
157   if (!*options) return 0;
158   ierr = PetscOptionsClear(*options);if (ierr) return ierr;
159   /* XXX what about monitors ? */
160   free(*options);
161   *options = NULL;
162   PetscFunctionReturn(0);
163 }
164 
165 /*
166     PetscOptionsCreateDefault - Creates the default global options database
167 */
168 PetscErrorCode PetscOptionsCreateDefault(void)
169 {
170   PetscErrorCode ierr;
171 
172   if (!defaultoptions) {
173     ierr = PetscOptionsCreate(&defaultoptions);if (ierr) return ierr;
174     initialoptions = defaultoptions;
175   }
176   return 0;
177 }
178 
179 /*@
180       PetscOptionsPush - Push a new PetscOptions object as the default provider of options
181 
182   Logically Collective
183 
184   Input Parameter:
185 .   opt - the options obtained with PetscOptionsCreate()
186 
187   Notes:
188   Use PetscOptionsPop() to return to the previous default options database
189   Allows using different parts of a code to use different options databases
190 
191 .seealso: PetscOptionsPop(), PetscOptionsCreate(), PetscOptionDestroy(), PetscOptionsInsert(), PetscOptionsSetValue()
192 
193 @*/
194 PetscErrorCode PetscOptionsPush(PetscOptions opt)
195 {
196   PetscErrorCode ierr;
197 
198   PetscFunctionBegin;
199   if (!defaultoptions) {
200     ierr = PetscOptionsCreateDefault();CHKERRQ(ierr);
201   }
202   defaultoptions->next = opt;
203   opt->previous        = defaultoptions;
204   defaultoptions       = opt;
205   PetscFunctionReturn(0);
206 }
207 
208 /*@
209       PetscOptionsPop - Pop the most recent PetscOptionsPush() to return to the previous default options
210 
211   Logically Collective
212 
213   Notes:
214   Use PetscOptionsPop() to return to the previous default options database
215   Allows using different parts of a code to use different options databases
216 
217 .seealso: PetscOptionsPop(), PetscOptionsCreate(), PetscOptionDestroy(), PetscOptionsInsert(), PetscOptionsSetValue()
218 
219 @*/
220 PetscErrorCode PetscOptionsPop(void)
221 {
222   PetscFunctionBegin;
223   if (!defaultoptions) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing default options");
224   if (!defaultoptions->previous) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"PetscOptionsPop() called too many times");
225   defaultoptions = defaultoptions->previous;
226   PetscFunctionReturn(0);
227 }
228 
229 /*
230     PetscOptionsDestroyDefault - Destroys the default global options database
231 */
232 PetscErrorCode PetscOptionsDestroyDefault(void)
233 {
234   PetscErrorCode ierr;
235 
236   ierr = PetscOptionsDestroy(&defaultoptions);if (ierr) return ierr;
237   return 0;
238 }
239 
240 /*@C
241    PetscOptionsValidKey - PETSc Options database keys must begin with one or two dashes (-) followed by a letter.
242 
243    Input Parameter:
244 .  key - string to check if valid
245 
246    Output Parameter:
247 .  valid - PETSC_TRUE if a valid key
248 
249    Level: intermediate
250 @*/
251 PetscErrorCode PetscOptionsValidKey(const char key[],PetscBool *valid)
252 {
253   char           *ptr;
254 
255   PetscFunctionBegin;
256   if (key) PetscValidCharPointer(key,1);
257   PetscValidPointer(valid,2);
258   *valid = PETSC_FALSE;
259   if (!key) PetscFunctionReturn(0);
260   if (key[0] != '-') PetscFunctionReturn(0);
261   if (key[1] == '-') key++;
262   if (!isalpha((int)(key[1]))) PetscFunctionReturn(0);
263   (void) strtod(key,&ptr);
264   if (ptr != key && !(*ptr == '_' || isalnum(*ptr))) PetscFunctionReturn(0);
265   *valid = PETSC_TRUE;
266   PetscFunctionReturn(0);
267 }
268 
269 /*@C
270    PetscOptionsInsertString - Inserts options into the database from a string
271 
272    Not Collective, but only processes that call this routine will set the options
273    included in the string
274 
275    Input Parameter:
276 .  in_str - string that contains options separated by blanks
277 
278 
279    Level: intermediate
280 
281    Contributed by Boyana Norris
282 
283 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
284           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
285           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
286           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
287           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
288           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsInsertFile()
289 @*/
290 PetscErrorCode PetscOptionsInsertString(PetscOptions options,const char in_str[])
291 {
292   char           *first,*second;
293   PetscErrorCode ierr;
294   PetscToken     token;
295   PetscBool      key,ispush,ispop,isopts;
296 
297   PetscFunctionBegin;
298   ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr);
299   ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
300   while (first) {
301     ierr = PetscStrcasecmp(first,"-prefix_push",&ispush);CHKERRQ(ierr);
302     ierr = PetscStrcasecmp(first,"-prefix_pop",&ispop);CHKERRQ(ierr);
303     ierr = PetscStrcasecmp(first,"-options_file",&isopts);CHKERRQ(ierr);
304     ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr);
305     if (ispush) {
306       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
307       ierr = PetscOptionsPrefixPush(options,second);CHKERRQ(ierr);
308       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
309     } else if (ispop) {
310       ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr);
311       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
312     } else if (isopts) {
313       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
314       ierr = PetscOptionsInsertFile(PETSC_COMM_SELF,options,second,PETSC_TRUE);CHKERRQ(ierr);
315       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
316     } else if (key) {
317       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
318       ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr);
319       if (!key) {
320         ierr = PetscOptionsSetValue(options,first,second);CHKERRQ(ierr);
321         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
322       } else {
323         ierr  = PetscOptionsSetValue(options,first,NULL);CHKERRQ(ierr);
324         first = second;
325       }
326     } else {
327       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
328     }
329   }
330   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
331   PetscFunctionReturn(0);
332 }
333 
334 /*
335     Returns a line (ended by a \n, \r or null character of any length. Result should be freed with free()
336 */
337 static char *Petscgetline(FILE * f)
338 {
339   size_t size  = 0;
340   size_t len   = 0;
341   size_t last  = 0;
342   char   *buf  = NULL;
343 
344   if (feof(f)) return 0;
345   do {
346     size += 1024; /* BUFSIZ is defined as "the optimal read size for this platform" */
347     buf   = (char*)realloc((void*)buf,size); /* realloc(NULL,n) is the same as malloc(n) */
348     /* Actually do the read. Note that fgets puts a terminal '\0' on the
349     end of the string, so we make sure we overwrite this */
350     if (!fgets(buf+len,1024,f)) buf[len]=0;
351     PetscStrlen(buf,&len);
352     last = len - 1;
353   } while (!feof(f) && buf[last] != '\n' && buf[last] != '\r');
354   if (len) return buf;
355   free(buf);
356   return 0;
357 }
358 
359 /*@C
360      PetscOptionsInsertFile - Inserts options into the database from a file.
361 
362      Collective
363 
364   Input Parameter:
365 +   comm - the processes that will share the options (usually PETSC_COMM_WORLD)
366 .   options - options database, use NULL for default global database
367 .   file - name of file
368 -   require - if PETSC_TRUE will generate an error if the file does not exist
369 
370 
371   Notes:
372     Use  # for lines that are comments and which should be ignored.
373 
374    Usually, instead of using this command, one should list the file name in the call to PetscInitialize(), this insures that certain options
375    such as -log_view or -malloc_debug are processed properly. This routine only sets options into the options database that will be processed by later
376    calls to XXXSetFromOptions() it should not be used for options listed under PetscInitialize().
377 
378   Level: developer
379 
380 .seealso: PetscOptionsSetValue(), PetscOptionsView(), PetscOptionsHasName(), PetscOptionsGetInt(),
381           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
382           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
383           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
384           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
385           PetscOptionsFList(), PetscOptionsEList()
386 
387 @*/
388 PetscErrorCode PetscOptionsInsertFile(MPI_Comm comm,PetscOptions options,const char file[],PetscBool require)
389 {
390   char           *string,fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*vstring = 0,*astring = 0,*packed = 0;
391   PetscErrorCode ierr;
392   size_t         i,len,bytes;
393   FILE           *fd;
394   PetscToken     token;
395   int            err;
396   char           cmt[1]={'#'},*cmatch;
397   PetscMPIInt    rank,cnt=0,acnt=0,counts[2];
398   PetscBool      isdir;
399 
400   PetscFunctionBegin;
401   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
402   if (!rank) {
403     cnt        = 0;
404     acnt       = 0;
405 
406     ierr = PetscFixFilename(file,fname);CHKERRQ(ierr);
407     fd   = fopen(fname,"r");
408     ierr = PetscTestDirectory(fname,'r',&isdir);CHKERRQ(ierr);
409     if (isdir && require) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Specified options file %s is a directory",fname);
410     if (fd && !isdir) {
411       PetscSegBuffer vseg,aseg;
412       ierr = PetscSegBufferCreate(1,4000,&vseg);CHKERRQ(ierr);
413       ierr = PetscSegBufferCreate(1,2000,&aseg);CHKERRQ(ierr);
414 
415       /* the following line will not work when opening initial files (like .petscrc) since info is not yet set */
416       ierr = PetscInfo1(0,"Opened options file %s\n",file);CHKERRQ(ierr);
417 
418       while ((string = Petscgetline(fd))) {
419         /* eliminate comments from each line */
420         for (i=0; i<1; i++) {
421           ierr = PetscStrchr(string,cmt[i],&cmatch);CHKERRQ(ierr);
422           if (cmatch) *cmatch = 0;
423         }
424         ierr = PetscStrlen(string,&len);CHKERRQ(ierr);
425         /* replace tabs, ^M, \n with " " */
426         for (i=0; i<len; i++) {
427           if (string[i] == '\t' || string[i] == '\r' || string[i] == '\n') {
428             string[i] = ' ';
429           }
430         }
431         ierr = PetscTokenCreate(string,' ',&token);CHKERRQ(ierr);
432         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
433         if (!first) {
434           goto destroy;
435         } else if (!first[0]) { /* if first token is empty spaces, redo first token */
436           ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
437         }
438         ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
439         if (!first) {
440           goto destroy;
441         } else if (first[0] == '-') {
442           ierr = PetscStrlen(first,&len);CHKERRQ(ierr);
443           ierr = PetscSegBufferGet(vseg,len+1,&vstring);CHKERRQ(ierr);
444           ierr = PetscMemcpy(vstring,first,len);CHKERRQ(ierr);
445           vstring[len] = ' ';
446           if (second) {
447             ierr = PetscStrlen(second,&len);CHKERRQ(ierr);
448             ierr = PetscSegBufferGet(vseg,len+3,&vstring);CHKERRQ(ierr);
449             vstring[0] = '"';
450             ierr = PetscMemcpy(vstring+1,second,len);CHKERRQ(ierr);
451             vstring[len+1] = '"';
452             vstring[len+2] = ' ';
453           }
454         } else {
455           PetscBool match;
456 
457           ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr);
458           if (match) {
459             ierr = PetscTokenFind(token,&third);CHKERRQ(ierr);
460             if (!third) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second);
461             ierr = PetscStrlen(second,&len);CHKERRQ(ierr);
462             ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr);
463             ierr = PetscMemcpy(astring,second,len);CHKERRQ(ierr);
464             astring[len] = ' ';
465 
466             ierr = PetscStrlen(third,&len);CHKERRQ(ierr);
467             ierr = PetscSegBufferGet(aseg,len+1,&astring);CHKERRQ(ierr);
468             ierr = PetscMemcpy(astring,third,len);CHKERRQ(ierr);
469             astring[len] = ' ';
470           } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown statement in options file: (%s)",string);
471         }
472 destroy:
473         free(string);
474         ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
475       }
476       err = fclose(fd);
477       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
478       ierr = PetscSegBufferGetSize(aseg,&bytes);CHKERRQ(ierr); /* size without null termination */
479       ierr = PetscMPIIntCast(bytes,&acnt);CHKERRQ(ierr);
480       ierr = PetscSegBufferGet(aseg,1,&astring);CHKERRQ(ierr);
481       astring[0] = 0;
482       ierr = PetscSegBufferGetSize(vseg,&bytes);CHKERRQ(ierr); /* size without null termination */
483       ierr = PetscMPIIntCast(bytes,&cnt);CHKERRQ(ierr);
484       ierr = PetscSegBufferGet(vseg,1,&vstring);CHKERRQ(ierr);
485       vstring[0] = 0;
486       ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr);
487       ierr = PetscSegBufferExtractTo(aseg,packed);CHKERRQ(ierr);
488       ierr = PetscSegBufferExtractTo(vseg,packed+acnt+1);CHKERRQ(ierr);
489       ierr = PetscSegBufferDestroy(&aseg);CHKERRQ(ierr);
490       ierr = PetscSegBufferDestroy(&vseg);CHKERRQ(ierr);
491     } else if (require) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Unable to open Options File %s",fname);
492   }
493 
494   counts[0] = acnt;
495   counts[1] = cnt;
496   ierr = MPI_Bcast(counts,2,MPI_INT,0,comm);CHKERRQ(ierr);
497   acnt = counts[0];
498   cnt = counts[1];
499   if (rank) {
500     ierr = PetscMalloc1(2+acnt+cnt,&packed);CHKERRQ(ierr);
501   }
502   if (acnt || cnt) {
503     ierr = MPI_Bcast(packed,2+acnt+cnt,MPI_CHAR,0,comm);CHKERRQ(ierr);
504     astring = packed;
505     vstring = packed + acnt + 1;
506   }
507 
508   if (acnt) {
509     PetscToken token;
510     char       *first,*second;
511 
512     ierr = PetscTokenCreate(astring,' ',&token);CHKERRQ(ierr);
513     ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
514     while (first) {
515       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
516       ierr = PetscOptionsSetAlias(options,first,second);CHKERRQ(ierr);
517       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
518     }
519     ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
520   }
521 
522   if (cnt) {
523     ierr = PetscOptionsInsertString(options,vstring);CHKERRQ(ierr);
524   }
525   ierr = PetscFree(packed);CHKERRQ(ierr);
526   PetscFunctionReturn(0);
527 }
528 
529 static PetscErrorCode PetscOptionsInsertArgs(PetscOptions options,int argc,char *args[])
530 {
531   PetscErrorCode ierr;
532   int            left    = argc - 1;
533   char           **eargs = args + 1;
534 
535   PetscFunctionBegin;
536   while (left) {
537     PetscBool isoptions_file,isprefixpush,isprefixpop,isp4,tisp4,isp4yourname,isp4rmrank,key;
538     ierr = PetscStrcasecmp(eargs[0],"-options_file",&isoptions_file);CHKERRQ(ierr);
539     ierr = PetscStrcasecmp(eargs[0],"-prefix_push",&isprefixpush);CHKERRQ(ierr);
540     ierr = PetscStrcasecmp(eargs[0],"-prefix_pop",&isprefixpop);CHKERRQ(ierr);
541     ierr = PetscStrcasecmp(eargs[0],"-p4pg",&isp4);CHKERRQ(ierr);
542     ierr = PetscStrcasecmp(eargs[0],"-p4yourname",&isp4yourname);CHKERRQ(ierr);
543     ierr = PetscStrcasecmp(eargs[0],"-p4rmrank",&isp4rmrank);CHKERRQ(ierr);
544     ierr = PetscStrcasecmp(eargs[0],"-p4wd",&tisp4);CHKERRQ(ierr);
545     isp4 = (PetscBool) (isp4 || tisp4);
546     ierr = PetscStrcasecmp(eargs[0],"-np",&tisp4);CHKERRQ(ierr);
547     isp4 = (PetscBool) (isp4 || tisp4);
548     ierr = PetscStrcasecmp(eargs[0],"-p4amslave",&tisp4);CHKERRQ(ierr);
549     ierr = PetscOptionsValidKey(eargs[0],&key);CHKERRQ(ierr);
550 
551     if (!key) {
552       eargs++; left--;
553     } else if (isoptions_file) {
554       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
555       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing filename for -options_file filename option");
556       ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,eargs[1],PETSC_TRUE);CHKERRQ(ierr);
557       eargs += 2; left -= 2;
558     } else if (isprefixpush) {
559       if (left <= 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option");
560       if (eargs[1][0] == '-') SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Missing prefix for -prefix_push option (prefixes cannot start with '-')");
561       ierr = PetscOptionsPrefixPush(options,eargs[1]);CHKERRQ(ierr);
562       eargs += 2; left -= 2;
563     } else if (isprefixpop) {
564       ierr = PetscOptionsPrefixPop(options);CHKERRQ(ierr);
565       eargs++; left--;
566 
567       /*
568        These are "bad" options that MPICH, etc put on the command line
569        we strip them out here.
570        */
571     } else if (tisp4 || isp4rmrank) {
572       eargs += 1; left -= 1;
573     } else if (isp4 || isp4yourname) {
574       eargs += 2; left -= 2;
575     } else {
576       PetscBool nextiskey = PETSC_FALSE;
577       if (left >= 2) {ierr = PetscOptionsValidKey(eargs[1],&nextiskey);CHKERRQ(ierr);}
578       if (left < 2 || nextiskey) {
579         ierr = PetscOptionsSetValue(options,eargs[0],NULL);CHKERRQ(ierr);
580         eargs++; left--;
581       } else {
582         ierr = PetscOptionsSetValue(options,eargs[0],eargs[1]);CHKERRQ(ierr);
583         eargs += 2; left -= 2;
584       }
585     }
586   }
587   PetscFunctionReturn(0);
588 }
589 
590 
591 /*@C
592    PetscOptionsInsert - Inserts into the options database from the command line,
593                    the environmental variable and a file.
594 
595    Input Parameters:
596 +  options - options database or NULL for the default global database
597 .  argc - count of number of command line arguments
598 .  args - the command line arguments
599 -  file - optional filename, defaults to ~username/.petscrc
600 
601    Note:
602    Since PetscOptionsInsert() is automatically called by PetscInitialize(),
603    the user does not typically need to call this routine. PetscOptionsInsert()
604    can be called several times, adding additional entries into the database.
605 
606    Options Database Keys:
607 +   -options_monitor <optional filename> - print options names and values as they are set
608 .   -options_file <filename> - read options from a file
609 
610    Level: advanced
611 
612 .seealso: PetscOptionsDestroy(), PetscOptionsView(), PetscOptionsInsertString(), PetscOptionsInsertFile(),
613           PetscInitialize()
614 @*/
615 PetscErrorCode PetscOptionsInsert(PetscOptions options,int *argc,char ***args,const char file[])
616 {
617   PetscErrorCode ierr;
618   PetscMPIInt    rank;
619   char           filename[PETSC_MAX_PATH_LEN];
620   PetscBool      flag = PETSC_FALSE;
621 
622 
623   PetscFunctionBegin;
624   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
625 
626   if (file && file[0]) {
627     ierr = PetscStrreplace(PETSC_COMM_WORLD,file,filename,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
628     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,filename,PETSC_TRUE);CHKERRQ(ierr);
629   }
630   /*
631      We want to be able to give -skip_petscrc on the command line, but need to parse it first.  Since the command line
632      should take precedence, we insert it twice.  It would be sufficient to just scan for -skip_petscrc.
633   */
634   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs(options,*argc,*args);CHKERRQ(ierr);}
635   ierr = PetscOptionsGetBool(NULL,NULL,"-skip_petscrc",&flag,NULL);CHKERRQ(ierr);
636   if (!flag) {
637     ierr = PetscGetHomeDirectory(filename,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
638     /* PetscOptionsInsertFile() does a fopen() on rank0 only - so only rank0 HomeDir value is relavent */
639     if (filename[0]) { ierr = PetscStrcat(filename,"/.petscrc");CHKERRQ(ierr); }
640     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,filename,PETSC_FALSE);CHKERRQ(ierr);
641     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,".petscrc",PETSC_FALSE);CHKERRQ(ierr);
642     ierr = PetscOptionsInsertFile(PETSC_COMM_WORLD,options,"petscrc",PETSC_FALSE);CHKERRQ(ierr);
643   }
644 
645   /* insert environment options */
646   {
647     char   *eoptions = NULL;
648     size_t len       = 0;
649     if (!rank) {
650       eoptions = (char*)getenv("PETSC_OPTIONS");
651       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
652       ierr     = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
653     } else {
654       ierr = MPI_Bcast(&len,1,MPIU_SIZE_T,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
655       if (len) {
656         ierr = PetscMalloc1(len+1,&eoptions);CHKERRQ(ierr);
657       }
658     }
659     if (len) {
660       ierr = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
661       if (rank) eoptions[len] = 0;
662       ierr = PetscOptionsInsertString(options,eoptions);CHKERRQ(ierr);
663       if (rank) {ierr = PetscFree(eoptions);CHKERRQ(ierr);}
664     }
665   }
666 
667 #if defined(PETSC_HAVE_YAML)
668   {
669     char      yaml_file[PETSC_MAX_PATH_LEN];
670     PetscBool yaml_flg;
671     ierr = PetscOptionsGetString(NULL,NULL,"-options_file_yaml",yaml_file,PETSC_MAX_PATH_LEN,&yaml_flg);CHKERRQ(ierr);
672     if (yaml_flg) {
673       ierr = PetscOptionsInsertFileYAML(PETSC_COMM_WORLD,yaml_file,PETSC_TRUE);CHKERRQ(ierr);
674     }
675   }
676 #endif
677 
678   /* insert command line options again because they take precedence over arguments in petscrc/environment */
679   if (argc && args && *argc) {ierr = PetscOptionsInsertArgs(options,*argc,*args);CHKERRQ(ierr);}
680   PetscFunctionReturn(0);
681 }
682 
683 /*@C
684    PetscOptionsView - Prints the options that have been loaded. This is
685    useful for debugging purposes.
686 
687    Logically Collective on PetscViewer
688 
689    Input Parameter:
690 -  options - options database, use NULL for default global database
691 +  viewer - must be an PETSCVIEWERASCII viewer
692 
693    Options Database Key:
694 .  -options_view - Activates PetscOptionsView() within PetscFinalize()
695 
696    Level: advanced
697 
698 .seealso: PetscOptionsAllUsed()
699 @*/
700 PetscErrorCode PetscOptionsView(PetscOptions options,PetscViewer viewer)
701 {
702   PetscErrorCode ierr;
703   PetscInt       i;
704   PetscBool      isascii;
705 
706   PetscFunctionBegin;
707   if (viewer) PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
708   options = options ? options : defaultoptions;
709   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
710   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
711   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only supports ASCII viewer");
712 
713   if (!options->N) {
714     ierr = PetscViewerASCIIPrintf(viewer,"#No PETSc Option Table entries\n");CHKERRQ(ierr);
715     PetscFunctionReturn(0);
716   }
717 
718   ierr = PetscViewerASCIIPrintf(viewer,"#PETSc Option Table entries:\n");CHKERRQ(ierr);
719   for (i=0; i<options->N; i++) {
720     if (options->values[i]) {
721       ierr = PetscViewerASCIIPrintf(viewer,"-%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
722     } else {
723       ierr = PetscViewerASCIIPrintf(viewer,"-%s\n",options->names[i]);CHKERRQ(ierr);
724     }
725   }
726   ierr = PetscViewerASCIIPrintf(viewer,"#End of PETSc Option Table entries\n");CHKERRQ(ierr);
727   PetscFunctionReturn(0);
728 }
729 
730 /*
731    Called by error handlers to print options used in run
732 */
733 PETSC_EXTERN PetscErrorCode PetscOptionsViewError(void)
734 {
735   PetscInt     i;
736   PetscOptions options = defaultoptions;
737 
738   PetscFunctionBegin;
739   if (options->N) {
740     (*PetscErrorPrintf)("PETSc Option Table entries:\n");
741   } else {
742     (*PetscErrorPrintf)("No PETSc Option Table entries\n");
743   }
744   for (i=0; i<options->N; i++) {
745     if (options->values[i]) {
746       (*PetscErrorPrintf)("-%s %s\n",options->names[i],options->values[i]);
747     } else {
748       (*PetscErrorPrintf)("-%s\n",options->names[i]);
749     }
750   }
751   PetscFunctionReturn(0);
752 }
753 
754 /*@C
755    PetscOptionsPrefixPush - Designate a prefix to be used by all options insertions to follow.
756 
757    Not Collective, but prefix will only be applied on calling ranks
758 
759    Input Parameter:
760 +  options - options database, or NULL for the default global database
761 -  prefix - The string to append to the existing prefix
762 
763    Options Database Keys:
764 +   -prefix_push <some_prefix_> - push the given prefix
765 -   -prefix_pop - pop the last prefix
766 
767    Notes:
768    It is common to use this in conjunction with -options_file as in
769 
770 $ -prefix_push system1_ -options_file system1rc -prefix_pop -prefix_push system2_ -options_file system2rc -prefix_pop
771 
772    where the files no longer require all options to be prefixed with -system2_.
773 
774 Level: advanced
775 
776 .seealso: PetscOptionsPrefixPop()
777 @*/
778 PetscErrorCode PetscOptionsPrefixPush(PetscOptions options,const char prefix[])
779 {
780   PetscErrorCode ierr;
781   size_t         n;
782   PetscInt       start;
783   char           key[MAXOPTNAME+1];
784   PetscBool      valid;
785 
786   PetscFunctionBegin;
787   PetscValidCharPointer(prefix,1);
788   options = options ? options : defaultoptions;
789   if (options->prefixind >= MAXPREFIXES) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum depth of prefix stack %d exceeded, recompile \n src/sys/objects/options.c with larger value for MAXPREFIXES",MAXPREFIXES);
790   key[0] = '-'; /* keys must start with '-' */
791   ierr = PetscStrncpy(key+1,prefix,sizeof(key)-1);CHKERRQ(ierr);
792   ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
793   if (!valid) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_USER,"Given prefix \"%s\" not valid (the first character must be a letter, do not include leading '-')",prefix);
794   start = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
795   ierr = PetscStrlen(prefix,&n);CHKERRQ(ierr);
796   if (n+1 > sizeof(options->prefix)-start) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Maximum prefix length %d exceeded",sizeof(options->prefix));
797   ierr = PetscMemcpy(options->prefix+start,prefix,n+1);CHKERRQ(ierr);
798   options->prefixstack[options->prefixind++] = start+n;
799   PetscFunctionReturn(0);
800 }
801 
802 /*@C
803    PetscOptionsPrefixPop - Remove the latest options prefix, see PetscOptionsPrefixPush() for details
804 
805    Not  Collective, but prefix will only be popped on calling ranks
806 
807   Input Parameters:
808 .  options - options database, or NULL for the default global database
809 
810    Level: advanced
811 
812 .seealso: PetscOptionsPrefixPush()
813 @*/
814 PetscErrorCode PetscOptionsPrefixPop(PetscOptions options)
815 {
816   PetscInt offset;
817 
818   PetscFunctionBegin;
819   options = options ? options : defaultoptions;
820   if (options->prefixind < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More prefixes popped than pushed");
821   options->prefixind--;
822   offset = options->prefixind ? options->prefixstack[options->prefixind-1] : 0;
823   options->prefix[offset] = 0;
824   PetscFunctionReturn(0);
825 }
826 
827 /*@C
828     PetscOptionsClear - Removes all options form the database leaving it empty.
829 
830   Input Parameters:
831 .  options - options database, use NULL for the default global database
832 
833    Level: developer
834 
835 .seealso: PetscOptionsInsert()
836 @*/
837 PetscErrorCode PetscOptionsClear(PetscOptions options)
838 {
839   PetscInt i;
840 
841   options = options ? options : defaultoptions;
842   if (!options) return 0;
843 
844   for (i=0; i<options->N; i++) {
845     if (options->names[i])  free(options->names[i]);
846     if (options->values[i]) free(options->values[i]);
847   }
848   options->N = 0;
849 
850   for (i=0; i<options->Naliases; i++) {
851     free(options->aliases1[i]);
852     free(options->aliases2[i]);
853   }
854   options->Naliases = 0;
855 
856   /* destroy hash table */
857   kh_destroy(HO,options->ht);
858   options->ht = NULL;
859 
860   options->prefixind = 0;
861   options->prefix[0] = 0;
862   options->help      = PETSC_FALSE;
863   return 0;
864 }
865 
866 /*@C
867    PetscOptionsSetAlias - Makes a key and alias for another key
868 
869    Not Collective, but setting values on certain processors could cause problems
870    for parallel objects looking for options.
871 
872    Input Parameters:
873 +  options - options database, or NULL for default global database
874 .  newname - the alias
875 -  oldname - the name that alias will refer to
876 
877    Level: advanced
878 
879 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
880           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
881           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
882           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
883           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
884           PetscOptionsFList(), PetscOptionsEList()
885 @*/
886 PetscErrorCode PetscOptionsSetAlias(PetscOptions options,const char newname[],const char oldname[])
887 {
888   PetscInt       n;
889   size_t         len;
890   PetscErrorCode ierr;
891 
892   PetscFunctionBegin;
893   PetscValidCharPointer(newname,2);
894   PetscValidCharPointer(oldname,3);
895   options = options ? options : defaultoptions;
896   if (newname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliased must start with '-': Instead %s",newname);
897   if (oldname[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"aliasee must start with '-': Instead %s",oldname);
898 
899   n = options->Naliases;
900   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);
901 
902   newname++; oldname++;
903   ierr = PetscStrlen(newname,&len);CHKERRQ(ierr);
904   options->aliases1[n] = (char*)malloc((len+1)*sizeof(char));
905   ierr = PetscStrcpy(options->aliases1[n],newname);CHKERRQ(ierr);
906   ierr = PetscStrlen(oldname,&len);CHKERRQ(ierr);
907   options->aliases2[n] = (char*)malloc((len+1)*sizeof(char));
908   ierr = PetscStrcpy(options->aliases2[n],oldname);CHKERRQ(ierr);
909   options->Naliases++;
910   PetscFunctionReturn(0);
911 }
912 
913 /*@C
914    PetscOptionsSetValue - Sets an option name-value pair in the options
915    database, overriding whatever is already present.
916 
917    Not Collective, but setting values on certain processors could cause problems
918    for parallel objects looking for options.
919 
920    Input Parameters:
921 +  options - options database, use NULL for the default global database
922 .  name - name of option, this SHOULD have the - prepended
923 -  value - the option value (not used for all options, so can be NULL)
924 
925    Level: intermediate
926 
927    Note:
928    This function can be called BEFORE PetscInitialize()
929 
930    Developers Note: Uses malloc() directly because PETSc may not be initialized yet.
931 
932 .seealso: PetscOptionsInsert(), PetscOptionsClearValue()
933 @*/
934 PetscErrorCode PetscOptionsSetValue(PetscOptions options,const char name[],const char value[])
935 {
936   size_t         len;
937   int            N,n,i;
938   char           **names;
939   char           fullname[MAXOPTNAME] = "";
940   PetscErrorCode ierr;
941 
942   if (!options && !defaultoptions) {
943     ierr = PetscOptionsCreateDefault();if (ierr) return ierr;
944   }
945   options = options ? options : defaultoptions;
946 
947   if (name[0] != '-') return PETSC_ERR_ARG_OUTOFRANGE;
948 
949   /* this is so that -h and -help are equivalent (p4 does not like -help)*/
950   if (!strcmp(name,"-h")) name = "-help";
951   if (!PetscOptNameCmp(name,"-help")) options->help = PETSC_TRUE;
952 
953   name++; /* skip starting dash */
954 
955   if (options->prefixind > 0) {
956     strncpy(fullname,options->prefix,sizeof(fullname));
957     fullname[sizeof(fullname)-1] = 0;
958     strncat(fullname,name,sizeof(fullname)-strlen(fullname)-1);
959     fullname[sizeof(fullname)-1] = 0;
960     name = fullname;
961   }
962 
963   /* check against aliases */
964   N = options->Naliases;
965   for (i=0; i<N; i++) {
966     int result = PetscOptNameCmp(options->aliases1[i],name);
967     if (!result) { name = options->aliases2[i]; break; }
968   }
969 
970   /* slow search */
971   N = n = options->N;
972   names = options->names;
973   for (i=0; i<N; i++) {
974     int result = PetscOptNameCmp(names[i],name);
975     if (!result) {
976       n = i; goto setvalue;
977     } else if (result > 0) {
978       n = i; break;
979     }
980   }
981   if (N >= MAXOPTIONS) return PETSC_ERR_MEM;
982   /* shift remaining values up 1 */
983   for (i=N; i>n; i--) {
984     options->names[i]  = options->names[i-1];
985     options->values[i] = options->values[i-1];
986     options->used[i]   = options->used[i-1];
987   }
988   options->names[n]  = NULL;
989   options->values[n] = NULL;
990   options->used[n]   = PETSC_FALSE;
991   options->N++;
992 
993   /* destroy hash table */
994   kh_destroy(HO,options->ht);
995   options->ht = NULL;
996 
997   /* set new name */
998   len = strlen(name);
999   options->names[n] = (char*)malloc((len+1)*sizeof(char));
1000   if (!options->names[n]) return PETSC_ERR_MEM;
1001   strcpy(options->names[n],name);
1002 
1003 setvalue:
1004   /* set new value */
1005   if (options->values[n]) free(options->values[n]);
1006   len = value ? strlen(value) : 0;
1007   if (len) {
1008     options->values[n] = (char*)malloc((len+1)*sizeof(char));
1009     if (!options->values[n]) return PETSC_ERR_MEM;
1010     strcpy(options->values[n],value);
1011   } else {
1012     options->values[n] = NULL;
1013   }
1014 
1015   ierr = PetscOptionsMonitor(options,name,value?value:"");if (ierr) return ierr;
1016   return 0;
1017 }
1018 
1019 /*@C
1020    PetscOptionsClearValue - Clears an option name-value pair in the options
1021    database, overriding whatever is already present.
1022 
1023    Not Collective, but setting values on certain processors could cause problems
1024    for parallel objects looking for options.
1025 
1026    Input Parameter:
1027 +  options - options database, use NULL for the default global database
1028 .  name - name of option, this SHOULD have the - prepended
1029 
1030    Level: intermediate
1031 
1032 .seealso: PetscOptionsInsert()
1033 @*/
1034 PetscErrorCode PetscOptionsClearValue(PetscOptions options,const char name[])
1035 {
1036   int            N,n,i;
1037   char           **names;
1038   PetscErrorCode ierr;
1039 
1040   PetscFunctionBegin;
1041   options = options ? options : defaultoptions;
1042   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name);
1043 
1044   /* this is so that -h and -help are equivalent (p4 does not like -help)*/
1045   if (!strcmp(name,"-h")) name = "-help";
1046   if (!PetscOptNameCmp(name,"-help")) options->help = PETSC_FALSE;
1047 
1048   name++; /* skip starting dash */
1049 
1050   /* slow search */
1051   N = n = options->N;
1052   names = options->names;
1053   for (i=0; i<N; i++) {
1054     int result = PetscOptNameCmp(names[i],name);
1055     if (!result) {
1056       n = i; break;
1057     } else if (result > 0) {
1058       n = N; break;
1059     }
1060   }
1061   if (n == N) PetscFunctionReturn(0); /* it was not present */
1062 
1063   /* remove name and value */
1064   if (options->names[n])  free(options->names[n]);
1065   if (options->values[n]) free(options->values[n]);
1066   /* shift remaining values down 1 */
1067   for (i=n; i<N-1; i++) {
1068     options->names[i]  = options->names[i+1];
1069     options->values[i] = options->values[i+1];
1070     options->used[i]   = options->used[i+1];
1071   }
1072   options->N--;
1073 
1074   /* destroy hash table */
1075   kh_destroy(HO,options->ht);
1076   options->ht = NULL;
1077 
1078   ierr = PetscOptionsMonitor(options,name,NULL);CHKERRQ(ierr);
1079   PetscFunctionReturn(0);
1080 }
1081 
1082 /*@C
1083    PetscOptionsFindPair - Gets an option name-value pair from the options database.
1084 
1085    Not Collective
1086 
1087    Input Parameters:
1088 +  options - options database, use NULL for the default global database
1089 .  pre - the string to prepend to the name or NULL, this SHOULD NOT have the "-" prepended
1090 -  name - name of option, this SHOULD have the "-" prepended
1091 
1092    Output Parameters:
1093 +  value - the option value (optional, not used for all options)
1094 -  set - whether the option is set (optional)
1095 
1096    Level: developer
1097 
1098 .seealso: PetscOptionsSetValue(), PetscOptionsClearValue()
1099 @*/
1100 PetscErrorCode PetscOptionsFindPair(PetscOptions options,const char pre[],const char name[],const char *value[],PetscBool *set)
1101 {
1102   char           buf[MAXOPTNAME];
1103   PetscBool      usehashtable = PETSC_TRUE;
1104   PetscBool      matchnumbers = PETSC_TRUE;
1105   PetscErrorCode ierr;
1106 
1107   PetscFunctionBegin;
1108   options = options ? options : defaultoptions;
1109   if (pre && PetscUnlikely(pre[0] == '-')) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix cannot begin with '-': Instead %s",pre);
1110   if (PetscUnlikely(name[0] != '-')) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name);
1111 
1112   name++; /* skip starting dash */
1113 
1114   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1115   if (pre && pre[0]) {
1116     char *ptr = buf;
1117     if (name[0] == '-') { *ptr++ = '-';  name++; }
1118     ierr = PetscStrncpy(ptr,pre,buf+sizeof(buf)-ptr);CHKERRQ(ierr);
1119     ierr = PetscStrlcat(buf,name,sizeof(buf));CHKERRQ(ierr);
1120     name = buf;
1121   }
1122 
1123 #if defined(PETSC_USE_DEBUG)
1124   {
1125     PetscBool valid;
1126     char      key[MAXOPTNAME+1] = "-";
1127     ierr = PetscStrncpy(key+1,name,sizeof(key)-1);CHKERRQ(ierr);
1128     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1129     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1130   }
1131 #endif
1132 
1133   if (!options->ht && usehashtable) {
1134     int i,ret;
1135     khiter_t it;
1136     khash_t(HO) *ht;
1137     ht = kh_init(HO);
1138     if (PetscUnlikely(!ht)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed");
1139     ret = kh_resize(HO,ht,options->N*2); /* twice the required size to reduce risk of collisions */
1140     if (PetscUnlikely(ret)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed");
1141     for (i=0; i<options->N; i++) {
1142       it = kh_put(HO,ht,options->names[i],&ret);
1143       if (PetscUnlikely(ret != 1)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Hash table allocation failed");
1144       kh_val(ht,it) = i;
1145     }
1146     options->ht = ht;
1147   }
1148 
1149   if (usehashtable)
1150   { /* fast search */
1151     khash_t(HO) *ht = options->ht;
1152     khiter_t it = kh_get(HO,ht,name);
1153     if (it != kh_end(ht)) {
1154       int i = kh_val(ht,it);
1155       options->used[i]  = PETSC_TRUE;
1156       if (value) *value = options->values[i];
1157       if (set)   *set   = PETSC_TRUE;
1158       PetscFunctionReturn(0);
1159     }
1160   } else
1161   { /* slow search */
1162     int i, N = options->N;
1163     for (i=0; i<N; i++) {
1164       int result = PetscOptNameCmp(options->names[i],name);
1165       if (!result) {
1166         options->used[i]  = PETSC_TRUE;
1167         if (value) *value = options->values[i];
1168         if (set)   *set   = PETSC_TRUE;
1169         PetscFunctionReturn(0);
1170       } else if (result > 0) {
1171         break;
1172       }
1173     }
1174   }
1175 
1176   /*
1177    The following block slows down all lookups in the most frequent path (most lookups are unsuccessful).
1178    Maybe this special lookup mode should be enabled on request with a push/pop API.
1179    The feature of matching _%d_ used sparingly in the codebase.
1180    */
1181   if (matchnumbers) {
1182     int i,j,cnt = 0,locs[16],loce[16];
1183     /* determine the location and number of all _%d_ in the key */
1184     for (i=0; name[i]; i++) {
1185       if (name[i] == '_') {
1186         for (j=i+1; name[j]; j++) {
1187           if (name[j] >= '0' && name[j] <= '9') continue;
1188           if (name[j] == '_' && j > i+1) { /* found a number */
1189             locs[cnt]   = i+1;
1190             loce[cnt++] = j+1;
1191           }
1192           i = j-1;
1193           break;
1194         }
1195       }
1196     }
1197     for (i=0; i<cnt; i++) {
1198       PetscBool found;
1199       char      opt[MAXOPTNAME+1] = "-", tmp[MAXOPTNAME];
1200       ierr = PetscStrncpy(tmp,name,PetscMin((size_t)(locs[i]+1),sizeof(tmp)));CHKERRQ(ierr);
1201       ierr = PetscStrlcat(opt,tmp,sizeof(opt));CHKERRQ(ierr);
1202       ierr = PetscStrlcat(opt,name+loce[i],sizeof(opt));CHKERRQ(ierr);
1203       ierr = PetscOptionsFindPair(options,NULL,opt,value,&found);CHKERRQ(ierr);
1204       if (found) {if (set) *set = PETSC_TRUE; PetscFunctionReturn(0);}
1205     }
1206   }
1207 
1208   if (set) *set = PETSC_FALSE;
1209   PetscFunctionReturn(0);
1210 }
1211 
1212 /* Check whether any option begins with pre+name */
1213 PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions options,const char pre[], const char name[],const char *value[],PetscBool *set)
1214 {
1215   char           buf[MAXOPTNAME];
1216   int            numCnt = 0, locs[16],loce[16];
1217   PetscErrorCode ierr;
1218 
1219   PetscFunctionBegin;
1220   options = options ? options : defaultoptions;
1221   if (pre && pre[0] == '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Prefix cannot begin with '-': Instead %s",pre);
1222   if (name[0] != '-') SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Name must begin with '-': Instead %s",name);
1223 
1224   name++; /* skip starting dash */
1225 
1226   /* append prefix to name, if prefix="foo_" and option='--bar", prefixed option is --foo_bar */
1227   if (pre && pre[0]) {
1228     char *ptr = buf;
1229     if (name[0] == '-') { *ptr++ = '-';  name++; }
1230     ierr = PetscStrncpy(ptr,pre,sizeof(buf)+(size_t)(ptr-buf));CHKERRQ(ierr);
1231     ierr = PetscStrlcat(buf,name,sizeof(buf));CHKERRQ(ierr);
1232     name = buf;
1233   }
1234 
1235 #if defined(PETSC_USE_DEBUG)
1236   {
1237     PetscBool valid;
1238     char      key[MAXOPTNAME+1] = "-";
1239     ierr = PetscStrncpy(key+1,name,sizeof(key)-1);CHKERRQ(ierr);
1240     ierr = PetscOptionsValidKey(key,&valid);CHKERRQ(ierr);
1241     if (!valid) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid option '%s' obtained from pre='%s' and name='%s'",key,pre?pre:"",name);
1242   }
1243 #endif
1244 
1245   /* determine the location and number of all _%d_ in the key */
1246   {
1247     int i,j;
1248     for (i=0; name[i]; i++) {
1249       if (name[i] == '_') {
1250         for (j=i+1; name[j]; j++) {
1251           if (name[j] >= '0' && name[j] <= '9') continue;
1252           if (name[j] == '_' && j > i+1) { /* found a number */
1253             locs[numCnt]   = i+1;
1254             loce[numCnt++] = j+1;
1255           }
1256           i = j-1;
1257           break;
1258         }
1259       }
1260     }
1261   }
1262 
1263   { /* slow search */
1264     int       c, i;
1265     size_t    len;
1266     PetscBool match;
1267 
1268     for (c = -1; c < numCnt; ++c) {
1269       char opt[MAXOPTNAME+1] = "", tmp[MAXOPTNAME];
1270 
1271       if (c < 0) {
1272         ierr = PetscStrcpy(opt,name);CHKERRQ(ierr);
1273       } else {
1274         ierr = PetscStrncpy(tmp,name,PetscMin((size_t)(locs[c]+1),sizeof(tmp)));CHKERRQ(ierr);
1275         ierr = PetscStrlcat(opt,tmp,sizeof(opt));CHKERRQ(ierr);
1276         ierr = PetscStrlcat(opt,name+loce[c],sizeof(opt));CHKERRQ(ierr);
1277       }
1278       ierr = PetscStrlen(opt,&len);CHKERRQ(ierr);
1279       for (i=0; i<options->N; i++) {
1280         ierr = PetscStrncmp(options->names[i],opt,len,&match);CHKERRQ(ierr);
1281         if (match) {
1282           options->used[i]  = PETSC_TRUE;
1283           if (value) *value = options->values[i];
1284           if (set)   *set   = PETSC_TRUE;
1285           PetscFunctionReturn(0);
1286         }
1287       }
1288     }
1289   }
1290 
1291   if (set) *set = PETSC_FALSE;
1292   PetscFunctionReturn(0);
1293 }
1294 
1295 /*@C
1296    PetscOptionsReject - Generates an error if a certain option is given.
1297 
1298    Not Collective, but setting values on certain processors could cause problems
1299    for parallel objects looking for options.
1300 
1301    Input Parameters:
1302 +  options - options database, use NULL for default global database
1303 .  pre - the option prefix (may be NULL)
1304 .  name - the option name one is seeking
1305 -  mess - error message (may be NULL)
1306 
1307    Level: advanced
1308 
1309 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),OptionsHasName(),
1310           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1311           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1312           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1313           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1314           PetscOptionsFList(), PetscOptionsEList()
1315 @*/
1316 PetscErrorCode PetscOptionsReject(PetscOptions options,const char pre[],const char name[],const char mess[])
1317 {
1318   PetscErrorCode ierr;
1319   PetscBool      flag = PETSC_FALSE;
1320 
1321   PetscFunctionBegin;
1322   ierr = PetscOptionsHasName(options,pre,name,&flag);CHKERRQ(ierr);
1323   if (flag) {
1324     if (mess && mess[0]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: -%s%s with %s",pre?pre:"",name+1,mess);
1325     else SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Program has disabled option: -%s%s",pre?pre:"",name+1);
1326   }
1327   PetscFunctionReturn(0);
1328 }
1329 
1330 /*@C
1331    PetscOptionsHasHelp - Determines whether the "-help" option is in the database.
1332 
1333    Not Collective
1334 
1335    Input Parameters:
1336 .  options - options database, use NULL for default global database
1337 
1338    Output Parameters:
1339 .  set - PETSC_TRUE if found else PETSC_FALSE.
1340 
1341    Level: advanced
1342 
1343 .seealso: PetscOptionsHasName()
1344 @*/
1345 PetscErrorCode PetscOptionsHasHelp(PetscOptions options,PetscBool *set)
1346 {
1347   PetscFunctionBegin;
1348   PetscValidPointer(set,2);
1349   options = options ? options : defaultoptions;
1350   *set = options->help;
1351   PetscFunctionReturn(0);
1352 }
1353 
1354 /*@C
1355    PetscOptionsHasName - Determines whether a certain option is given in the database. This returns true whether the option is a number, string or boolean, even
1356                       its value is set to false.
1357 
1358    Not Collective
1359 
1360    Input Parameters:
1361 +  options - options database, use NULL for default global database
1362 .  pre - string to prepend to the name or NULL
1363 -  name - the option one is seeking
1364 
1365    Output Parameters:
1366 .  set - PETSC_TRUE if found else PETSC_FALSE.
1367 
1368    Level: beginner
1369 
1370    Notes:
1371    Name cannot be simply "-h".
1372 
1373    In many cases you probably want to use PetscOptionsGetBool() instead of calling this, to allowing toggling values.
1374 
1375 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
1376           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
1377           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1378           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1379           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1380           PetscOptionsFList(), PetscOptionsEList()
1381 @*/
1382 PetscErrorCode PetscOptionsHasName(PetscOptions options,const char pre[],const char name[],PetscBool *set)
1383 {
1384   const char     *value;
1385   PetscErrorCode ierr;
1386   PetscBool      flag;
1387 
1388   PetscFunctionBegin;
1389   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
1390   if (set) *set = flag;
1391   PetscFunctionReturn(0);
1392 }
1393 
1394 /*@C
1395    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
1396 
1397    Not Collective
1398 
1399    Input Paramter:
1400 .  options - the options database, use NULL for the default global database
1401 
1402    Output Parameter:
1403 .  copts - pointer where string pointer is stored
1404 
1405    Notes:
1406     the array and each entry in the array should be freed with PetscFree()
1407 
1408    Level: advanced
1409 
1410 .seealso: PetscOptionsAllUsed(), PetscOptionsView()
1411 @*/
1412 PetscErrorCode PetscOptionsGetAll(PetscOptions options,char *copts[])
1413 {
1414   PetscErrorCode ierr;
1415   PetscInt       i;
1416   size_t         len = 1,lent = 0;
1417   char           *coptions = NULL;
1418 
1419   PetscFunctionBegin;
1420   PetscValidPointer(copts,2);
1421   options = options ? options : defaultoptions;
1422   /* count the length of the required string */
1423   for (i=0; i<options->N; i++) {
1424     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
1425     len += 2 + lent;
1426     if (options->values[i]) {
1427       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
1428       len += 1 + lent;
1429     }
1430   }
1431   ierr = PetscMalloc1(len,&coptions);CHKERRQ(ierr);
1432   coptions[0] = 0;
1433   for (i=0; i<options->N; i++) {
1434     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
1435     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
1436     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
1437     if (options->values[i]) {
1438       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
1439       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
1440     }
1441   }
1442   *copts = coptions;
1443   PetscFunctionReturn(0);
1444 }
1445 
1446 /*@C
1447    PetscOptionsUsed - Indicates if PETSc has used a particular option set in the database
1448 
1449    Not Collective
1450 
1451    Input Parameter:
1452 +  options - options database, use NULL for default global database
1453 -  name - string name of option
1454 
1455    Output Parameter:
1456 .  used - PETSC_TRUE if the option was used, otherwise false, including if option was not found in options database
1457 
1458    Level: advanced
1459 
1460 .seealso: PetscOptionsView(), PetscOptionsLeft(), PetscOptionsAllUsed()
1461 @*/
1462 PetscErrorCode PetscOptionsUsed(PetscOptions options,const char *name,PetscBool *used)
1463 {
1464   PetscInt       i;
1465   PetscErrorCode ierr;
1466 
1467   PetscFunctionBegin;
1468   PetscValidCharPointer(name,2);
1469   PetscValidPointer(used,3);
1470   options = options ? options : defaultoptions;
1471   *used = PETSC_FALSE;
1472   for (i=0; i<options->N; i++) {
1473     ierr = PetscStrcmp(options->names[i],name,used);CHKERRQ(ierr);
1474     if (*used) {
1475       *used = options->used[i];
1476       break;
1477     }
1478   }
1479   PetscFunctionReturn(0);
1480 }
1481 
1482 /*@
1483    PetscOptionsAllUsed - Returns a count of the number of options in the
1484    database that have never been selected.
1485 
1486    Not Collective
1487 
1488    Input Parameter:
1489 .  options - options database, use NULL for default global database
1490 
1491    Output Parameter:
1492 .  N - count of options not used
1493 
1494    Level: advanced
1495 
1496 .seealso: PetscOptionsView()
1497 @*/
1498 PetscErrorCode PetscOptionsAllUsed(PetscOptions options,PetscInt *N)
1499 {
1500   PetscInt     i,n = 0;
1501 
1502   PetscFunctionBegin;
1503   PetscValidIntPointer(N,2);
1504   options = options ? options : defaultoptions;
1505   for (i=0; i<options->N; i++) {
1506     if (!options->used[i]) n++;
1507   }
1508   *N = n;
1509   PetscFunctionReturn(0);
1510 }
1511 
1512 /*@
1513    PetscOptionsLeft - Prints to screen any options that were set and never used.
1514 
1515    Not Collective
1516 
1517    Input Parameter:
1518 .  options - options database; use NULL for default global database
1519 
1520    Options Database Key:
1521 .  -options_left - Activates OptionsAllUsed() within PetscFinalize()
1522 
1523    Level: advanced
1524 
1525 .seealso: PetscOptionsAllUsed()
1526 @*/
1527 PetscErrorCode PetscOptionsLeft(PetscOptions options)
1528 {
1529   PetscErrorCode ierr;
1530   PetscInt       i;
1531 
1532   PetscFunctionBegin;
1533   options = options ? options : defaultoptions;
1534   for (i=0; i<options->N; i++) {
1535     if (!options->used[i]) {
1536       if (options->values[i]) {
1537         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s value: %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
1538       } else {
1539         ierr = PetscPrintf(PETSC_COMM_WORLD,"Option left: name:-%s (no value)\n",options->names[i]);CHKERRQ(ierr);
1540       }
1541     }
1542   }
1543   PetscFunctionReturn(0);
1544 }
1545 
1546 /*@C
1547    PetscOptionsLeftGet - Returns all options that were set and never used.
1548 
1549    Not Collective
1550 
1551    Input Parameter:
1552 .  options - options database, use NULL for default global database
1553 
1554    Output Parameter:
1555 .  N - count of options not used
1556 .  names - names of options not used
1557 .  values - values of options not used
1558 
1559    Level: advanced
1560 
1561    Notes:
1562    Users should call PetscOptionsLeftRestore() to free the memory allocated in this routine
1563 
1564 .seealso: PetscOptionsAllUsed(), PetscOptionsLeft()
1565 @*/
1566 PetscErrorCode PetscOptionsLeftGet(PetscOptions options,PetscInt *N,char **names[],char **values[])
1567 {
1568   PetscErrorCode ierr;
1569   PetscInt       i,n;
1570 
1571   PetscFunctionBegin;
1572   if (N) PetscValidIntPointer(N,2);
1573   if (names) PetscValidPointer(names,3);
1574   if (values) PetscValidPointer(values,4);
1575   options = options ? options : defaultoptions;
1576 
1577   /* The number of unused PETSc options */
1578   n = 0;
1579   for (i=0; i<options->N; i++) {
1580     if (!options->used[i]) n++;
1581   }
1582   if (N) { *N = n; }
1583   if (names)  { ierr = PetscMalloc1(n,names);CHKERRQ(ierr); }
1584   if (values) { ierr = PetscMalloc1(n,values);CHKERRQ(ierr); }
1585 
1586   n = 0;
1587   if (names || values) {
1588     for (i=0; i<options->N; i++) {
1589       if (!options->used[i]) {
1590         if (names)  (*names)[n]  = options->names[i];
1591         if (values) (*values)[n] = options->values[i];
1592         n++;
1593       }
1594     }
1595   }
1596   PetscFunctionReturn(0);
1597 }
1598 
1599 /*@C
1600    PetscOptionsLeftRestore - Free memory for the unused PETSc options obtained using PetscOptionsLeftGet.
1601 
1602    Not Collective
1603 
1604    Input Parameter:
1605 .  options - options database, use NULL for default global database
1606 .  names - names of options not used
1607 .  values - values of options not used
1608 
1609    Level: advanced
1610 
1611 .seealso: PetscOptionsAllUsed(), PetscOptionsLeft(), PetscOptionsLeftGet()
1612 @*/
1613 PetscErrorCode PetscOptionsLeftRestore(PetscOptions options,PetscInt *N,char **names[],char **values[])
1614 {
1615   PetscErrorCode ierr;
1616 
1617   PetscFunctionBegin;
1618   if (N) PetscValidIntPointer(N,2);
1619   if (names) PetscValidPointer(names,3);
1620   if (values) PetscValidPointer(values,4);
1621   if (N) { *N = 0; }
1622   if (names)  { ierr = PetscFree(*names);CHKERRQ(ierr); }
1623   if (values) { ierr = PetscFree(*values);CHKERRQ(ierr); }
1624   PetscFunctionReturn(0);
1625 }
1626 
1627 /*@C
1628    PetscOptionsSetFromOptions - Sets options related to the handling of options in PETSc
1629 
1630    Collective on PETSC_COMM_WORLD
1631 
1632    Input Parameter:
1633 .  options - options database, use NULL for default global database
1634 
1635    Options Database Keys:
1636 +  -options_monitor <optional filename> - prints the names and values of all runtime options as they are set. The monitor functionality is not
1637                 available for options set through a file, environment variable, or on
1638                 the command line. Only options set after PetscInitialize() completes will
1639                 be monitored.
1640 .  -options_monitor_cancel - cancel all options database monitors
1641 
1642    Notes:
1643    To see all options, run your program with the -help option or consult Users-Manual: sec_gettingstarted
1644 
1645    Level: intermediate
1646 
1647 @*/
1648 PetscErrorCode PetscOptionsSetFromOptions(PetscOptions options)
1649 {
1650   PetscBool      flgc = PETSC_FALSE,flgm;
1651   PetscErrorCode ierr;
1652   char           monfilename[PETSC_MAX_PATH_LEN];
1653   PetscViewer    monviewer;
1654 
1655   PetscFunctionBegin;
1656   /*
1657      The options argument is currently ignored since we currently maintain only a single options database
1658 
1659      options = options ? options : defaultoptions;
1660   */
1661   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Options for handling options","PetscOptions");CHKERRQ(ierr);
1662   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flgm);CHKERRQ(ierr);
1663   ierr = PetscOptionsBool("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",flgc,&flgc,NULL);CHKERRQ(ierr);
1664   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1665   if (flgm) {
1666     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
1667     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void**))PetscViewerDestroy);CHKERRQ(ierr);
1668   }
1669   if (flgc) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
1670   PetscFunctionReturn(0);
1671 }
1672 
1673 /*@C
1674    PetscOptionsMonitorDefault - Print all options set value events.
1675 
1676    Logically Collective on PETSC_COMM_WORLD
1677 
1678    Input Parameters:
1679 +  name  - option name string
1680 .  value - option value string
1681 -  ctx - an ASCII viewer
1682 
1683    Level: intermediate
1684 
1685 .seealso: PetscOptionsMonitorSet()
1686 @*/
1687 PetscErrorCode PetscOptionsMonitorDefault(const char name[],const char value[],void *ctx)
1688 {
1689   PetscErrorCode ierr;
1690   PetscViewer    viewer = (PetscViewer)ctx;
1691 
1692   PetscFunctionBegin;
1693   if (!value) {
1694     ierr = PetscViewerASCIIPrintf(viewer,"Removing option: %s\n",name,value);CHKERRQ(ierr);
1695   } else if (!value[0]) {
1696     ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s (no value)\n",name);CHKERRQ(ierr);
1697   } else {
1698     ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
1699   }
1700   PetscFunctionReturn(0);
1701 }
1702 
1703 /*@C
1704    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
1705    modified the PETSc options database.
1706 
1707    Not Collective
1708 
1709    Input Parameters:
1710 +  monitor - pointer to function (if this is NULL, it turns off monitoring
1711 .  mctx    - [optional] context for private data for the
1712              monitor routine (use NULL if no context is desired)
1713 -  monitordestroy - [optional] routine that frees monitor context
1714           (may be NULL)
1715 
1716    Calling Sequence of monitor:
1717 $     monitor (const char name[], const char value[], void *mctx)
1718 
1719 +  name - option name string
1720 .  value - option value string
1721 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
1722 
1723    Options Database Keys:
1724 +    -options_monitor    - sets PetscOptionsMonitorDefault()
1725 -    -options_monitor_cancel - cancels all monitors that have
1726                           been hardwired into a code by
1727                           calls to PetscOptionsMonitorSet(), but
1728                           does not cancel those set via
1729                           the options database.
1730 
1731    Notes:
1732    The default is to do nothing.  To print the name and value of options
1733    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
1734    with a null monitoring context.
1735 
1736    Several different monitoring routines may be set by calling
1737    PetscOptionsMonitorSet() multiple times; all will be called in the
1738    order in which they were set.
1739 
1740    Level: beginner
1741 
1742 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
1743 @*/
1744 PetscErrorCode PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void**))
1745 {
1746   PetscOptions options = defaultoptions;
1747 
1748   PetscFunctionBegin;
1749   if (options->numbermonitors >= MAXOPTIONSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
1750   options->monitor[options->numbermonitors]          = monitor;
1751   options->monitordestroy[options->numbermonitors]   = monitordestroy;
1752   options->monitorcontext[options->numbermonitors++] = (void*)mctx;
1753   PetscFunctionReturn(0);
1754 }
1755 
1756 /*@
1757    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
1758 
1759    Not Collective
1760 
1761    Options Database Key:
1762 .  -options_monitor_cancel - Cancels all monitors that have
1763     been hardwired into a code by calls to PetscOptionsMonitorSet(),
1764     but does not cancel those set via the options database.
1765 
1766    Level: intermediate
1767 
1768 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
1769 @*/
1770 PetscErrorCode PetscOptionsMonitorCancel(void)
1771 {
1772   PetscErrorCode ierr;
1773   PetscInt       i;
1774   PetscOptions   options = defaultoptions;
1775 
1776   PetscFunctionBegin;
1777   for (i=0; i<options->numbermonitors; i++) {
1778     if (options->monitordestroy[i]) {
1779       ierr = (*options->monitordestroy[i])(&options->monitorcontext[i]);CHKERRQ(ierr);
1780     }
1781   }
1782   options->numbermonitors = 0;
1783   PetscFunctionReturn(0);
1784 }
1785 
1786 /*
1787    PetscOptionsStringToBool - Converts string to PetscBool , handles cases like "yes", "no", "true", "false", "0", "1", "off", "on".
1788 */
1789 PetscErrorCode PetscOptionsStringToBool(const char value[],PetscBool *a)
1790 {
1791   PetscBool      istrue,isfalse;
1792   size_t         len;
1793   PetscErrorCode ierr;
1794 
1795   PetscFunctionBegin;
1796   ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
1797   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Character string of length zero has no logical value");
1798   ierr = PetscStrcasecmp(value,"TRUE",&istrue);CHKERRQ(ierr);
1799   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
1800   ierr = PetscStrcasecmp(value,"YES",&istrue);CHKERRQ(ierr);
1801   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
1802   ierr = PetscStrcasecmp(value,"1",&istrue);CHKERRQ(ierr);
1803   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
1804   ierr = PetscStrcasecmp(value,"on",&istrue);CHKERRQ(ierr);
1805   if (istrue) {*a = PETSC_TRUE; PetscFunctionReturn(0);}
1806   ierr = PetscStrcasecmp(value,"FALSE",&isfalse);CHKERRQ(ierr);
1807   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
1808   ierr = PetscStrcasecmp(value,"NO",&isfalse);CHKERRQ(ierr);
1809   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
1810   ierr = PetscStrcasecmp(value,"0",&isfalse);CHKERRQ(ierr);
1811   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
1812   ierr = PetscStrcasecmp(value,"off",&isfalse);CHKERRQ(ierr);
1813   if (isfalse) {*a = PETSC_FALSE; PetscFunctionReturn(0);}
1814   SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown logical value: %s",value);
1815 }
1816 
1817 /*
1818    PetscOptionsStringToInt - Converts a string to an integer value. Handles special cases such as "default" and "decide"
1819 */
1820 PetscErrorCode PetscOptionsStringToInt(const char name[],PetscInt *a)
1821 {
1822   PetscErrorCode ierr;
1823   size_t         len;
1824   PetscBool      decide,tdefault,mouse;
1825 
1826   PetscFunctionBegin;
1827   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
1828   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
1829 
1830   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
1831   if (!tdefault) {
1832     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
1833   }
1834   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
1835   if (!decide) {
1836     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
1837   }
1838   ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr);
1839 
1840   if (tdefault)    *a = PETSC_DEFAULT;
1841   else if (decide) *a = PETSC_DECIDE;
1842   else if (mouse)  *a = -1;
1843   else {
1844     char *endptr;
1845     long strtolval;
1846 
1847     strtolval = strtol(name,&endptr,10);
1848     if ((size_t) (endptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
1849 
1850 #if defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE_ATOLL)
1851     (void) strtolval;
1852     *a = atoll(name);
1853 #elif defined(PETSC_USE_64BIT_INDICES) && defined(PETSC_HAVE___INT64)
1854     (void) strtolval;
1855     *a = _atoi64(name);
1856 #else
1857     *a = (PetscInt)strtolval;
1858 #endif
1859   }
1860   PetscFunctionReturn(0);
1861 }
1862 
1863 #if defined(PETSC_USE_REAL___FLOAT128)
1864 #include <quadmath.h>
1865 #endif
1866 
1867 static PetscErrorCode PetscStrtod(const char name[],PetscReal *a,char **endptr)
1868 {
1869   PetscFunctionBegin;
1870 #if defined(PETSC_USE_REAL___FLOAT128)
1871   *a = strtoflt128(name,endptr);
1872 #else
1873   *a = (PetscReal)strtod(name,endptr);
1874 #endif
1875   PetscFunctionReturn(0);
1876 }
1877 
1878 static PetscErrorCode PetscStrtoz(const char name[],PetscScalar *a,char **endptr,PetscBool *isImaginary)
1879 {
1880   PetscBool      hasi = PETSC_FALSE;
1881   char           *ptr;
1882   PetscReal      strtoval;
1883   PetscErrorCode ierr;
1884 
1885   PetscFunctionBegin;
1886   ierr = PetscStrtod(name,&strtoval,&ptr);CHKERRQ(ierr);
1887   if (ptr == name) {
1888     strtoval = 1.;
1889     hasi = PETSC_TRUE;
1890     if (name[0] == 'i') {
1891       ptr++;
1892     } else if (name[0] == '+' && name[1] == 'i') {
1893       ptr += 2;
1894     } else if (name[0] == '-' && name[1] == 'i') {
1895       strtoval = -1.;
1896       ptr += 2;
1897     }
1898   } else if (*ptr == 'i') {
1899     hasi = PETSC_TRUE;
1900     ptr++;
1901   }
1902   *endptr = ptr;
1903   *isImaginary = hasi;
1904   if (hasi) {
1905 #if !defined(PETSC_USE_COMPLEX)
1906     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s contains imaginary but complex not supported ",name);
1907 #else
1908     *a = PetscCMPLX(0.,strtoval);
1909 #endif
1910   } else {
1911     *a = strtoval;
1912   }
1913   PetscFunctionReturn(0);
1914 }
1915 
1916 /*
1917    Converts a string to PetscReal value. Handles special cases like "default" and "decide"
1918 */
1919 PetscErrorCode PetscOptionsStringToReal(const char name[],PetscReal *a)
1920 {
1921   size_t         len;
1922   PetscBool      match;
1923   char           *endptr;
1924   PetscErrorCode ierr;
1925 
1926   PetscFunctionBegin;
1927   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
1928   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"String of length zero has no numerical value");
1929 
1930   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&match);CHKERRQ(ierr);
1931   if (!match) {
1932     ierr = PetscStrcasecmp(name,"DEFAULT",&match);CHKERRQ(ierr);
1933   }
1934   if (match) {*a = PETSC_DEFAULT; PetscFunctionReturn(0);}
1935 
1936   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&match);CHKERRQ(ierr);
1937   if (!match) {
1938     ierr = PetscStrcasecmp(name,"DECIDE",&match);CHKERRQ(ierr);
1939   }
1940   if (match) {*a = PETSC_DECIDE; PetscFunctionReturn(0);}
1941 
1942   ierr = PetscStrtod(name,a,&endptr);CHKERRQ(ierr);
1943   if ((size_t) (endptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value",name);
1944   PetscFunctionReturn(0);
1945 }
1946 
1947 PetscErrorCode PetscOptionsStringToScalar(const char name[],PetscScalar *a)
1948 {
1949   PetscBool      imag1;
1950   size_t         len;
1951   PetscScalar    val = 0.;
1952   char           *ptr = NULL;
1953   PetscErrorCode ierr;
1954 
1955   PetscFunctionBegin;
1956   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
1957   if (!len) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
1958   ierr = PetscStrtoz(name,&val,&ptr,&imag1);CHKERRQ(ierr);
1959 #if defined(PETSC_USE_COMPLEX)
1960   if ((size_t) (ptr - name) < len) {
1961     PetscBool   imag2;
1962     PetscScalar val2;
1963 
1964     ierr = PetscStrtoz(ptr,&val2,&ptr,&imag2);CHKERRQ(ierr);
1965     if (imag1 || !imag2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s: must specify imaginary component second",name);
1966     val = PetscCMPLX(PetscRealPart(val),PetscImaginaryPart(val2));
1967   }
1968 #endif
1969   if ((size_t) (ptr - name) != len) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
1970   *a = val;
1971   PetscFunctionReturn(0);
1972 }
1973 
1974 /*@C
1975    PetscOptionsGetBool - Gets the Logical (true or false) value for a particular
1976             option in the database.
1977 
1978    Not Collective
1979 
1980    Input Parameters:
1981 +  options - options database, use NULL for default global database
1982 .  pre - the string to prepend to the name or NULL
1983 -  name - the option one is seeking
1984 
1985    Output Parameter:
1986 +  ivalue - the logical value to return
1987 -  set - PETSC_TRUE  if found, else PETSC_FALSE
1988 
1989    Level: beginner
1990 
1991    Notes:
1992        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
1993        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
1994 
1995       If the option is given, but no value is provided, then ivalue and set are both given the value PETSC_TRUE. That is -requested_bool
1996      is equivalent to -requested_bool true
1997 
1998        If the user does not supply the option at all ivalue is NOT changed. Thus
1999      you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2000 
2001 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
2002           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsGetInt(), PetscOptionsBool(),
2003           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2004           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2005           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2006           PetscOptionsFList(), PetscOptionsEList()
2007 @*/
2008 PetscErrorCode PetscOptionsGetBool(PetscOptions options,const char pre[],const char name[],PetscBool *ivalue,PetscBool *set)
2009 {
2010   const char     *value;
2011   PetscBool      flag;
2012   PetscErrorCode ierr;
2013 
2014   PetscFunctionBegin;
2015   PetscValidCharPointer(name,3);
2016   if (ivalue) PetscValidIntPointer(ivalue,4);
2017   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2018   if (flag) {
2019     if (set) *set = PETSC_TRUE;
2020     if (!value) {
2021       if (ivalue) *ivalue = PETSC_TRUE;
2022     } else {
2023       ierr = PetscOptionsStringToBool(value, &flag);CHKERRQ(ierr);
2024       if (ivalue) *ivalue = flag;
2025     }
2026   } else {
2027     if (set) *set = PETSC_FALSE;
2028   }
2029   PetscFunctionReturn(0);
2030 }
2031 
2032 /*@C
2033    PetscOptionsGetEList - Puts a list of option values that a single one may be selected from
2034 
2035    Not Collective
2036 
2037    Input Parameters:
2038 +  options - options database, use NULL for default global database
2039 .  pre - the string to prepend to the name or NULL
2040 .  opt - option name
2041 .  list - the possible choices (one of these must be selected, anything else is invalid)
2042 .  ntext - number of choices
2043 
2044    Output Parameter:
2045 +  value - the index of the value to return (defaults to zero if the option name is given but no choice is listed)
2046 -  set - PETSC_TRUE if found, else PETSC_FALSE
2047 
2048    Level: intermediate
2049 
2050    Notes:
2051     If the user does not supply the option value is NOT changed. Thus
2052      you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2053 
2054    See PetscOptionsFList() for when the choices are given in a PetscFunctionList()
2055 
2056 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2057           PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2058           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2059           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2060           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2061           PetscOptionsFList(), PetscOptionsEList()
2062 @*/
2063 PetscErrorCode PetscOptionsGetEList(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscInt ntext,PetscInt *value,PetscBool *set)
2064 {
2065   PetscErrorCode ierr;
2066   size_t         alen,len = 0;
2067   char           *svalue;
2068   PetscBool      aset,flg = PETSC_FALSE;
2069   PetscInt       i;
2070 
2071   PetscFunctionBegin;
2072   PetscValidCharPointer(opt,3);
2073   for (i=0; i<ntext; i++) {
2074     ierr = PetscStrlen(list[i],&alen);CHKERRQ(ierr);
2075     if (alen > len) len = alen;
2076   }
2077   len += 5; /* a little extra space for user mistypes */
2078   ierr = PetscMalloc1(len,&svalue);CHKERRQ(ierr);
2079   ierr = PetscOptionsGetString(options,pre,opt,svalue,len,&aset);CHKERRQ(ierr);
2080   if (aset) {
2081     ierr = PetscEListFind(ntext,list,svalue,value,&flg);CHKERRQ(ierr);
2082     if (!flg) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown option %s for -%s%s",svalue,pre ? pre : "",opt+1);
2083     if (set) *set = PETSC_TRUE;
2084   } else if (set) *set = PETSC_FALSE;
2085   ierr = PetscFree(svalue);CHKERRQ(ierr);
2086   PetscFunctionReturn(0);
2087 }
2088 
2089 /*@C
2090    PetscOptionsGetEnum - Gets the enum value for a particular option in the database.
2091 
2092    Not Collective
2093 
2094    Input Parameters:
2095 +  options - options database, use NULL for default global database
2096 .  pre - option prefix or NULL
2097 .  opt - option name
2098 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
2099 -  defaultv - the default (current) value
2100 
2101    Output Parameter:
2102 +  value - the  value to return
2103 -  set - PETSC_TRUE if found, else PETSC_FALSE
2104 
2105    Level: beginner
2106 
2107    Notes:
2108     If the user does not supply the option value is NOT changed. Thus
2109      you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2110 
2111           List is usually something like PCASMTypes or some other predefined list of enum names
2112 
2113 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
2114           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
2115           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
2116           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2117           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2118           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2119           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
2120 @*/
2121 PetscErrorCode PetscOptionsGetEnum(PetscOptions options,const char pre[],const char opt[],const char * const *list,PetscEnum *value,PetscBool *set)
2122 {
2123   PetscErrorCode ierr;
2124   PetscInt       ntext = 0,tval;
2125   PetscBool      fset;
2126 
2127   PetscFunctionBegin;
2128   PetscValidCharPointer(opt,3);
2129   while (list[ntext++]) {
2130     if (ntext > 50) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument appears to be wrong or have more than 50 entries");
2131   }
2132   if (ntext < 3) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"List argument must have at least two entries: typename and type prefix");
2133   ntext -= 3;
2134   ierr = PetscOptionsGetEList(options,pre,opt,list,ntext,&tval,&fset);CHKERRQ(ierr);
2135   /* with PETSC_USE_64BIT_INDICES sizeof(PetscInt) != sizeof(PetscEnum) */
2136   if (fset) *value = (PetscEnum)tval;
2137   if (set) *set = fset;
2138   PetscFunctionReturn(0);
2139 }
2140 
2141 /*@C
2142    PetscOptionsGetInt - Gets the integer value for a particular option in the database.
2143 
2144    Not Collective
2145 
2146    Input Parameters:
2147 +  options - options database, use NULL for default global database
2148 .  pre - the string to prepend to the name or NULL
2149 -  name - the option one is seeking
2150 
2151    Output Parameter:
2152 +  ivalue - the integer value to return
2153 -  set - PETSC_TRUE if found, else PETSC_FALSE
2154 
2155    Level: beginner
2156 
2157    Notes:
2158    If the user does not supply the option ivalue is NOT changed. Thus
2159    you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2160 
2161 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
2162           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
2163           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
2164           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2165           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2166           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2167           PetscOptionsFList(), PetscOptionsEList()
2168 @*/
2169 PetscErrorCode PetscOptionsGetInt(PetscOptions options,const char pre[],const char name[],PetscInt *ivalue,PetscBool *set)
2170 {
2171   const char     *value;
2172   PetscErrorCode ierr;
2173   PetscBool      flag;
2174 
2175   PetscFunctionBegin;
2176   PetscValidCharPointer(name,3);
2177   PetscValidIntPointer(ivalue,4);
2178   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2179   if (flag) {
2180     if (!value) {
2181       if (set) *set = PETSC_FALSE;
2182     } else {
2183       if (set) *set = PETSC_TRUE;
2184       ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr);
2185     }
2186   } else {
2187     if (set) *set = PETSC_FALSE;
2188   }
2189   PetscFunctionReturn(0);
2190 }
2191 
2192 /*@C
2193    PetscOptionsGetReal - Gets the double precision value for a particular
2194    option in the database.
2195 
2196    Not Collective
2197 
2198    Input Parameters:
2199 +  options - options database, use NULL for default global database
2200 .  pre - string to prepend to each name or NULL
2201 -  name - the option one is seeking
2202 
2203    Output Parameter:
2204 +  dvalue - the double value to return
2205 -  set - PETSC_TRUE if found, PETSC_FALSE if not found
2206 
2207    Notes:
2208     If the user does not supply the option dvalue is NOT changed. Thus
2209      you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2210 
2211    Level: beginner
2212 
2213 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2214           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(),PetscOptionsBool(),
2215           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2216           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2217           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2218           PetscOptionsFList(), PetscOptionsEList()
2219 @*/
2220 PetscErrorCode PetscOptionsGetReal(PetscOptions options,const char pre[],const char name[],PetscReal *dvalue,PetscBool *set)
2221 {
2222   const char     *value;
2223   PetscBool      flag;
2224   PetscErrorCode ierr;
2225 
2226   PetscFunctionBegin;
2227   PetscValidCharPointer(name,3);
2228   PetscValidRealPointer(dvalue,4);
2229   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2230   if (flag) {
2231     if (!value) {
2232       if (set) *set = PETSC_FALSE;
2233     } else {
2234       if (set) *set = PETSC_TRUE;
2235       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
2236     }
2237   } else {
2238     if (set) *set = PETSC_FALSE;
2239   }
2240   PetscFunctionReturn(0);
2241 }
2242 
2243 /*@C
2244    PetscOptionsGetScalar - Gets the scalar value for a particular
2245    option in the database.
2246 
2247    Not Collective
2248 
2249    Input Parameters:
2250 +  options - options database, use NULL for default global database
2251 .  pre - string to prepend to each name or NULL
2252 -  name - the option one is seeking
2253 
2254    Output Parameter:
2255 +  dvalue - the double value to return
2256 -  set - PETSC_TRUE if found, else PETSC_FALSE
2257 
2258    Level: beginner
2259 
2260    Usage:
2261    A complex number 2+3i must be specified with NO spaces
2262 
2263    Notes:
2264     If the user does not supply the option dvalue is NOT changed. Thus
2265      you should ALWAYS initialize the ivalue if you access it without first checking if the set flag is true.
2266 
2267 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2268           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2269           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2270           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2271           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2272           PetscOptionsFList(), PetscOptionsEList()
2273 @*/
2274 PetscErrorCode PetscOptionsGetScalar(PetscOptions options,const char pre[],const char name[],PetscScalar *dvalue,PetscBool *set)
2275 {
2276   const char     *value;
2277   PetscBool      flag;
2278   PetscErrorCode ierr;
2279 
2280   PetscFunctionBegin;
2281   PetscValidCharPointer(name,3);
2282   PetscValidScalarPointer(dvalue,4);
2283   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2284   if (flag) {
2285     if (!value) {
2286       if (set) *set = PETSC_FALSE;
2287     } else {
2288 #if !defined(PETSC_USE_COMPLEX)
2289       ierr = PetscOptionsStringToReal(value,dvalue);CHKERRQ(ierr);
2290 #else
2291       ierr = PetscOptionsStringToScalar(value,dvalue);CHKERRQ(ierr);
2292 #endif
2293       if (set) *set = PETSC_TRUE;
2294     }
2295   } else { /* flag */
2296     if (set) *set = PETSC_FALSE;
2297   }
2298   PetscFunctionReturn(0);
2299 }
2300 
2301 /*@C
2302    PetscOptionsGetString - Gets the string value for a particular option in
2303    the database.
2304 
2305    Not Collective
2306 
2307    Input Parameters:
2308 +  options - options database, use NULL for default global database
2309 .  pre - string to prepend to name or NULL
2310 .  name - the option one is seeking
2311 -  len - maximum length of the string including null termination
2312 
2313    Output Parameters:
2314 +  string - location to copy string
2315 -  set - PETSC_TRUE if found, else PETSC_FALSE
2316 
2317    Level: beginner
2318 
2319    Fortran Note:
2320    The Fortran interface is slightly different from the C/C++
2321    interface (len is not used).  Sample usage in Fortran follows
2322 .vb
2323       character *20    string
2324       PetscErrorCode   ierr
2325       PetscBool        set
2326       call PetscOptionsGetString(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,'-s',string,set,ierr)
2327 .ve
2328 
2329    Notes:
2330     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
2331 
2332            If the user does not use the option then the string is not changed. Thus
2333            you should ALWAYS initialize the string if you access it without first checking if the set flag is true.
2334 
2335     Note:
2336       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).
2337 
2338 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2339           PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2340           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2341           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2342           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2343           PetscOptionsFList(), PetscOptionsEList()
2344 @*/
2345 PetscErrorCode PetscOptionsGetString(PetscOptions options,const char pre[],const char name[],char string[],size_t len,PetscBool *set)
2346 {
2347   const char     *value;
2348   PetscBool      flag;
2349   PetscErrorCode ierr;
2350 
2351   PetscFunctionBegin;
2352   PetscValidCharPointer(name,3);
2353   PetscValidCharPointer(string,4);
2354   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
2355   if (!flag) {
2356     if (set) *set = PETSC_FALSE;
2357   } else {
2358     if (set) *set = PETSC_TRUE;
2359     if (value) {
2360       ierr = PetscStrncpy(string,value,len);CHKERRQ(ierr);
2361     } else {
2362       ierr = PetscMemzero(string,len);CHKERRQ(ierr);
2363     }
2364   }
2365   PetscFunctionReturn(0);
2366 }
2367 
2368 char *PetscOptionsGetStringMatlab(PetscOptions options,const char pre[],const char name[])
2369 {
2370   const char     *value;
2371   PetscBool      flag;
2372   PetscErrorCode ierr;
2373 
2374   PetscFunctionBegin;
2375   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);if (ierr) PetscFunctionReturn(0);
2376   if (flag) PetscFunctionReturn((char*)value);
2377   else PetscFunctionReturn(0);
2378 }
2379 
2380 /*@C
2381    PetscOptionsGetBoolArray - Gets an array of Logical (true or false) values for a particular
2382    option in the database.  The values must be separated with commas with
2383    no intervening spaces.
2384 
2385    Not Collective
2386 
2387    Input Parameters:
2388 +  options - options database, use NULL for default global database
2389 .  pre - string to prepend to each name or NULL
2390 .  name - the option one is seeking
2391 -  nmax - maximum number of values to retrieve
2392 
2393    Output Parameter:
2394 +  dvalue - the integer values to return
2395 .  nmax - actual number of values retreived
2396 -  set - PETSC_TRUE if found, else PETSC_FALSE
2397 
2398    Level: beginner
2399 
2400    Notes:
2401        TRUE, true, YES, yes, nostring, and 1 all translate to PETSC_TRUE
2402        FALSE, false, NO, no, and 0 all translate to PETSC_FALSE
2403 
2404 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2405           PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2406           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2407           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2408           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2409           PetscOptionsFList(), PetscOptionsEList()
2410 @*/
2411 PetscErrorCode PetscOptionsGetBoolArray(PetscOptions options,const char pre[],const char name[],PetscBool dvalue[],PetscInt *nmax,PetscBool *set)
2412 {
2413   const char     *svalue;
2414   char           *value;
2415   PetscErrorCode ierr;
2416   PetscInt       n = 0;
2417   PetscBool      flag;
2418   PetscToken     token;
2419 
2420   PetscFunctionBegin;
2421   PetscValidCharPointer(name,3);
2422   PetscValidIntPointer(dvalue,4);
2423   PetscValidIntPointer(nmax,5);
2424 
2425   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2426   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2427   if (set) *set = PETSC_TRUE;
2428   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2429   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2430   while (value && n < *nmax) {
2431     ierr = PetscOptionsStringToBool(value,dvalue);CHKERRQ(ierr);
2432     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2433     dvalue++;
2434     n++;
2435   }
2436   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2437   *nmax = n;
2438   PetscFunctionReturn(0);
2439 }
2440 
2441 /*@C
2442    PetscOptionsGetEnumArray - Gets an array of enum values for a particular option in the database.
2443 
2444    Not Collective
2445 
2446    Input Parameters:
2447 +  options - options database, use NULL for default global database
2448 .  pre - option prefix or NULL
2449 .  name - option name
2450 .  list - array containing the list of choices, followed by the enum name, followed by the enum prefix, followed by a null
2451 -  nmax - maximum number of values to retrieve
2452 
2453    Output Parameters:
2454 +  ivalue - the  enum values to return
2455 .  nmax - actual number of values retreived
2456 -  set - PETSC_TRUE if found, else PETSC_FALSE
2457 
2458    Level: beginner
2459 
2460    Notes:
2461    The array must be passed as a comma separated list.
2462 
2463    There must be no intervening spaces between the values.
2464 
2465    list is usually something like PCASMTypes or some other predefined list of enum names.
2466 
2467 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(), PetscOptionsGetInt(),
2468           PetscOptionsGetEnum(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
2469           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(), PetscOptionsName(),
2470           PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(), PetscOptionsStringArray(),PetscOptionsRealArray(),
2471           PetscOptionsScalar(), PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2472           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsGetEList(), PetscOptionsEnum()
2473 @*/
2474 PetscErrorCode PetscOptionsGetEnumArray(PetscOptions options,const char pre[],const char name[],const char *const *list,PetscEnum ivalue[],PetscInt *nmax,PetscBool *set)
2475 {
2476   const char     *svalue;
2477   char           *value;
2478   PetscInt       n = 0;
2479   PetscEnum      evalue;
2480   PetscBool      flag;
2481   PetscToken     token;
2482   PetscErrorCode ierr;
2483 
2484   PetscFunctionBegin;
2485   PetscValidCharPointer(name,3);
2486   PetscValidPointer(list,4);
2487   PetscValidPointer(ivalue,5);
2488   PetscValidIntPointer(nmax,6);
2489 
2490   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2491   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2492   if (set) *set = PETSC_TRUE;
2493   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2494   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2495   while (value && n < *nmax) {
2496     ierr = PetscEnumFind(list,value,&evalue,&flag);CHKERRQ(ierr);
2497     if (!flag) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_USER,"Unknown enum value '%s' for -%s%s",svalue,pre ? pre : "",name+1);
2498     ivalue[n++] = evalue;
2499     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2500   }
2501   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
2502   *nmax = n;
2503   PetscFunctionReturn(0);
2504 }
2505 
2506 /*@C
2507    PetscOptionsGetIntArray - Gets an array of integer values for a particular
2508    option in the database.
2509 
2510    Not Collective
2511 
2512    Input Parameters:
2513 +  options - options database, use NULL for default global database
2514 .  pre - string to prepend to each name or NULL
2515 .  name - the option one is seeking
2516 -  nmax - maximum number of values to retrieve
2517 
2518    Output Parameter:
2519 +  ivalue - the integer values to return
2520 .  nmax - actual number of values retreived
2521 -  set - PETSC_TRUE if found, else PETSC_FALSE
2522 
2523    Level: beginner
2524 
2525    Notes:
2526    The array can be passed as
2527    a comma separated list:                                 0,1,2,3,4,5,6,7
2528    a range (start-end+1):                                  0-8
2529    a range with given increment (start-end+1:inc):         0-7:2
2530    a combination of values and ranges separated by commas: 0,1-8,8-15:2
2531 
2532    There must be no intervening spaces between the values.
2533 
2534 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2535           PetscOptionsGetString(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2536           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2537           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2538           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2539           PetscOptionsFList(), PetscOptionsEList()
2540 @*/
2541 PetscErrorCode PetscOptionsGetIntArray(PetscOptions options,const char pre[],const char name[],PetscInt ivalue[],PetscInt *nmax,PetscBool *set)
2542 {
2543   const char     *svalue;
2544   char           *value;
2545   PetscErrorCode ierr;
2546   PetscInt       n = 0,i,j,start,end,inc,nvalues;
2547   size_t         len;
2548   PetscBool      flag,foundrange;
2549   PetscToken     token;
2550 
2551   PetscFunctionBegin;
2552   PetscValidCharPointer(name,3);
2553   PetscValidIntPointer(ivalue,4);
2554   PetscValidIntPointer(nmax,5);
2555 
2556   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2557   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2558   if (set) *set = PETSC_TRUE;
2559   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2560   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2561   while (value && n < *nmax) {
2562     /* look for form  d-D where d and D are integers */
2563     foundrange = PETSC_FALSE;
2564     ierr       = PetscStrlen(value,&len);CHKERRQ(ierr);
2565     if (value[0] == '-') i=2;
2566     else i=1;
2567     for (;i<(int)len; i++) {
2568       if (value[i] == '-') {
2569         if (i == (int)len-1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_USER,"Error in %D-th array entry %s\n",n,value);
2570         value[i] = 0;
2571 
2572         ierr = PetscOptionsStringToInt(value,&start);CHKERRQ(ierr);
2573         inc  = 1;
2574         j    = i+1;
2575         for (;j<(int)len; j++) {
2576           if (value[j] == ':') {
2577             value[j] = 0;
2578 
2579             ierr = PetscOptionsStringToInt(value+j+1,&inc);CHKERRQ(ierr);
2580             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);
2581             break;
2582           }
2583         }
2584         ierr = PetscOptionsStringToInt(value+i+1,&end);CHKERRQ(ierr);
2585         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);
2586         nvalues = (end-start)/inc + (end-start)%inc;
2587         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);
2588         for (;start<end; start+=inc) {
2589           *ivalue = start; ivalue++;n++;
2590         }
2591         foundrange = PETSC_TRUE;
2592         break;
2593       }
2594     }
2595     if (!foundrange) {
2596       ierr = PetscOptionsStringToInt(value,ivalue);CHKERRQ(ierr);
2597       ivalue++;
2598       n++;
2599     }
2600     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2601   }
2602   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2603   *nmax = n;
2604   PetscFunctionReturn(0);
2605 }
2606 
2607 /*@C
2608    PetscOptionsGetRealArray - Gets an array of double precision values for a
2609    particular option in the database.  The values must be separated with
2610    commas with no intervening spaces.
2611 
2612    Not Collective
2613 
2614    Input Parameters:
2615 +  options - options database, use NULL for default global database
2616 .  pre - string to prepend to each name or NULL
2617 .  name - the option one is seeking
2618 -  nmax - maximum number of values to retrieve
2619 
2620    Output Parameters:
2621 +  dvalue - the double values to return
2622 .  nmax - actual number of values retreived
2623 -  set - PETSC_TRUE if found, else PETSC_FALSE
2624 
2625    Level: beginner
2626 
2627 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2628           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
2629           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2630           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2631           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2632           PetscOptionsFList(), PetscOptionsEList()
2633 @*/
2634 PetscErrorCode PetscOptionsGetRealArray(PetscOptions options,const char pre[],const char name[],PetscReal dvalue[],PetscInt *nmax,PetscBool *set)
2635 {
2636   const char     *svalue;
2637   char           *value;
2638   PetscErrorCode ierr;
2639   PetscInt       n = 0;
2640   PetscBool      flag;
2641   PetscToken     token;
2642 
2643   PetscFunctionBegin;
2644   PetscValidCharPointer(name,3);
2645   PetscValidRealPointer(dvalue,4);
2646   PetscValidIntPointer(nmax,5);
2647 
2648   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2649   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2650   if (set) *set = PETSC_TRUE;
2651   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2652   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2653   while (value && n < *nmax) {
2654     ierr = PetscOptionsStringToReal(value,dvalue++);CHKERRQ(ierr);
2655     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2656     n++;
2657   }
2658   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2659   *nmax = n;
2660   PetscFunctionReturn(0);
2661 }
2662 
2663 /*@C
2664    PetscOptionsGetScalarArray - Gets an array of scalars for a
2665    particular option in the database.  The values must be separated with
2666    commas with no intervening spaces.
2667 
2668    Not Collective
2669 
2670    Input Parameters:
2671 +  options - options database, use NULL for default global database
2672 .  pre - string to prepend to each name or NULL
2673 .  name - the option one is seeking
2674 -  nmax - maximum number of values to retrieve
2675 
2676    Output Parameters:
2677 +  dvalue - the scalar values to return
2678 .  nmax - actual number of values retreived
2679 -  set - PETSC_TRUE if found, else PETSC_FALSE
2680 
2681    Level: beginner
2682 
2683 .seealso: PetscOptionsGetInt(), PetscOptionsHasName(),
2684           PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsBool(),
2685           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2686           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2687           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2688           PetscOptionsFList(), PetscOptionsEList()
2689 @*/
2690 PetscErrorCode PetscOptionsGetScalarArray(PetscOptions options,const char pre[],const char name[],PetscScalar dvalue[],PetscInt *nmax,PetscBool *set)
2691 {
2692   const char     *svalue;
2693   char           *value;
2694   PetscErrorCode ierr;
2695   PetscInt       n = 0;
2696   PetscBool      flag;
2697   PetscToken     token;
2698 
2699   PetscFunctionBegin;
2700   PetscValidCharPointer(name,3);
2701   PetscValidRealPointer(dvalue,4);
2702   PetscValidIntPointer(nmax,5);
2703 
2704   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2705   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2706   if (set) *set = PETSC_TRUE;
2707   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2708   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2709   while (value && n < *nmax) {
2710     ierr = PetscOptionsStringToScalar(value,dvalue++);CHKERRQ(ierr);
2711     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2712     n++;
2713   }
2714   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2715   *nmax = n;
2716   PetscFunctionReturn(0);
2717 }
2718 
2719 /*@C
2720    PetscOptionsGetStringArray - Gets an array of string values for a particular
2721    option in the database. The values must be separated with commas with
2722    no intervening spaces.
2723 
2724    Not Collective
2725 
2726    Input Parameters:
2727 +  options - options database, use NULL for default global database
2728 .  pre - string to prepend to name or NULL
2729 .  name - the option one is seeking
2730 -  nmax - maximum number of strings
2731 
2732    Output Parameter:
2733 +  strings - location to copy strings
2734 -  set - PETSC_TRUE if found, else PETSC_FALSE
2735 
2736    Level: beginner
2737 
2738    Notes:
2739    The user should pass in an array of pointers to char, to hold all the
2740    strings returned by this function.
2741 
2742    The user is responsible for deallocating the strings that are
2743    returned. The Fortran interface for this routine is not supported.
2744 
2745    Contributed by Matthew Knepley.
2746 
2747 .seealso: PetscOptionsGetInt(), PetscOptionsGetReal(),
2748           PetscOptionsHasName(), PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool(),
2749           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
2750           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
2751           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
2752           PetscOptionsFList(), PetscOptionsEList()
2753 @*/
2754 PetscErrorCode PetscOptionsGetStringArray(PetscOptions options,const char pre[],const char name[],char *strings[],PetscInt *nmax,PetscBool *set)
2755 {
2756   const char     *svalue;
2757   char           *value;
2758   PetscErrorCode ierr;
2759   PetscInt       n = 0;
2760   PetscBool      flag;
2761   PetscToken     token;
2762 
2763   PetscFunctionBegin;
2764   PetscValidCharPointer(name,3);
2765   PetscValidPointer(strings,4);
2766   PetscValidIntPointer(nmax,5);
2767 
2768   ierr = PetscOptionsFindPair(options,pre,name,&svalue,&flag);CHKERRQ(ierr);
2769   if (!flag || !svalue)  { if (set) *set = PETSC_FALSE; *nmax = 0; PetscFunctionReturn(0);}
2770   if (set) *set = PETSC_TRUE;
2771   ierr = PetscTokenCreate(svalue,',',&token);CHKERRQ(ierr);
2772   ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2773   while (value && n < *nmax) {
2774     ierr = PetscStrallocpy(value,&strings[n]);CHKERRQ(ierr);
2775     ierr = PetscTokenFind(token,&value);CHKERRQ(ierr);
2776     n++;
2777   }
2778   ierr  = PetscTokenDestroy(&token);CHKERRQ(ierr);
2779   *nmax = n;
2780   PetscFunctionReturn(0);
2781 }
2782 
2783 /*@C
2784    PetscOptionsDeprecated - mark an option as deprecated, optionally replacing it with a new one
2785 
2786    Prints a deprecation warning, unless an option is supplied to suppress.
2787 
2788    Not Collective
2789 
2790    Input Parameters:
2791 +  pre - string to prepend to name or NULL
2792 .  oldname - the old, deprecated option
2793 .  newname - the new option, or NULL if option is purely removed
2794 .  version - a string describing the version of first deprecation, e.g. "3.9"
2795 -  info - additional information string, or NULL.
2796 
2797    Options Database Keys:
2798 . -options_suppress_deprecated_warnings - do not print deprecation warnings
2799 
2800    Notes:
2801    Must be called between PetscOptionsBegin() and PetscOptionsEnd().
2802    If newname is provided, the old option is replaced. Otherwise, it remains
2803    in the options database.
2804    If an option is not replaced, the info argument should be used to advise the user
2805    on how to proceed.
2806    There is a limit on the length of the warning printed, so very long strings
2807    provided as info may be truncated.
2808 
2809    Level: developer
2810 
2811 .seealso: PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsScalar(), PetscOptionsBool(), PetscOptionsString(), PetscOptionsSetValue()
2812 
2813 @*/
2814 PetscErrorCode PetscOptionsDeprecated_Private(PetscOptionItems *PetscOptionsObject,const char oldname[],const char newname[],const char version[],const char info[])
2815 {
2816   PetscErrorCode     ierr;
2817   PetscBool          found,quiet;
2818   const char         *value;
2819   const char * const quietopt="-options_suppress_deprecated_warnings";
2820   char               msg[4096];
2821 
2822   PetscFunctionBegin;
2823   PetscValidCharPointer(oldname,2);
2824   PetscValidCharPointer(version,4);
2825 
2826   ierr = PetscOptionsFindPair(PetscOptionsObject->options,PetscOptionsObject->prefix,oldname,&value,&found);CHKERRQ(ierr);
2827   if (found) {
2828     if (newname) {
2829       if (PetscOptionsObject->prefix) {
2830         ierr = PetscOptionsPrefixPush(PetscOptionsObject->options,PetscOptionsObject->prefix);CHKERRQ(ierr);
2831       }
2832       ierr = PetscOptionsSetValue(PetscOptionsObject->options,newname,value);CHKERRQ(ierr);
2833       if (PetscOptionsObject->prefix) {
2834         ierr = PetscOptionsPrefixPop(PetscOptionsObject->options);CHKERRQ(ierr);
2835       }
2836       ierr = PetscOptionsClearValue(PetscOptionsObject->options,oldname);CHKERRQ(ierr);
2837     }
2838     quiet = PETSC_FALSE;
2839     ierr = PetscOptionsGetBool(PetscOptionsObject->options,NULL,quietopt,&quiet,NULL);CHKERRQ(ierr);
2840     if (!quiet) {
2841       ierr = PetscStrcpy(msg,"** PETSc DEPRECATION WARNING ** : the option ");CHKERRQ(ierr);
2842       ierr = PetscStrcat(msg,oldname);CHKERRQ(ierr);
2843       ierr = PetscStrcat(msg," is deprecated as of version ");CHKERRQ(ierr);
2844       ierr = PetscStrcat(msg,version);CHKERRQ(ierr);
2845       ierr = PetscStrcat(msg," and will be removed in a future release.");CHKERRQ(ierr);
2846       if (newname) {
2847         ierr = PetscStrcat(msg," Please use the option ");CHKERRQ(ierr);
2848         ierr = PetscStrcat(msg,newname);CHKERRQ(ierr);
2849         ierr = PetscStrcat(msg," instead.");CHKERRQ(ierr);
2850       }
2851       if (info) {
2852         ierr = PetscStrcat(msg," ");CHKERRQ(ierr);
2853         ierr = PetscStrcat(msg,info);CHKERRQ(ierr);
2854       }
2855       ierr = PetscStrcat(msg," (Silence this warning with ");CHKERRQ(ierr);
2856       ierr = PetscStrcat(msg,quietopt);CHKERRQ(ierr);
2857       ierr = PetscStrcat(msg,")\n");CHKERRQ(ierr);
2858       ierr = PetscPrintf(PetscOptionsObject->comm,msg);CHKERRQ(ierr);
2859     }
2860   }
2861   PetscFunctionReturn(0);
2862 }
2863