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