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