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