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