xref: /petsc/src/sys/objects/options.c (revision da9f1d6b25924a16baf1fafcd5e58fa8eaafd3cf)
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 extern PetscOptionsObjectType PetscOptionsObject;
50 
51 /*
52     Options events monitor
53 */
54 #define PetscOptionsMonitor(name,value)                                     \
55         { PetscErrorCode _ierr; PetscInt _i,_im = options->numbermonitors; \
56           for (_i=0; _i<_im; _i++) {\
57             _ierr = (*options->monitor[_i])(name, value, options->monitorcontext[_i]);CHKERRQ(_ierr); \
58 	  } \
59 	}
60 
61 #undef __FUNCT__
62 #define __FUNCT__ "PetscOptionsAtoi"
63 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtoi(const char name[],PetscInt *a)
64 {
65   PetscErrorCode ierr;
66   size_t         i,len;
67   PetscTruth     decide,tdefault,mouse;
68 
69   PetscFunctionBegin;
70   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
71   if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
72 
73   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
74   if (!tdefault) {
75     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
76   }
77   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
78   if (!decide) {
79     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
80   }
81   ierr = PetscStrcasecmp(name,"mouse",&mouse);CHKERRQ(ierr);
82 
83   if (tdefault) {
84     *a = PETSC_DEFAULT;
85   } else if (decide) {
86     *a = PETSC_DECIDE;
87   } else if (mouse) {
88     *a = -1;
89   } else {
90     if (name[0] != '+' && name[0] != '-' && name[0] < '0' && name[0] > '9') {
91       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
92     }
93     for (i=1; i<len; i++) {
94       if (name[i] < '0' || name[i] > '9') {
95         SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no integer value (do not include . in it)",name);
96       }
97     }
98     *a  = atoi(name);
99   }
100   PetscFunctionReturn(0);
101 }
102 
103 #undef __FUNCT__
104 #define __FUNCT__ "PetscOptionsAtod"
105 PetscErrorCode PETSC_DLLEXPORT PetscOptionsAtod(const char name[],PetscReal *a)
106 {
107   PetscErrorCode ierr;
108   size_t         len;
109   PetscTruth     decide,tdefault;
110 
111   PetscFunctionBegin;
112   ierr = PetscStrlen(name,&len);CHKERRQ(ierr);
113   if (!len) SETERRQ(PETSC_ERR_ARG_WRONG,"character string of length zero has no numerical value");
114 
115   ierr = PetscStrcasecmp(name,"PETSC_DEFAULT",&tdefault);CHKERRQ(ierr);
116   if (!tdefault) {
117     ierr = PetscStrcasecmp(name,"DEFAULT",&tdefault);CHKERRQ(ierr);
118   }
119   ierr = PetscStrcasecmp(name,"PETSC_DECIDE",&decide);CHKERRQ(ierr);
120   if (!decide) {
121     ierr = PetscStrcasecmp(name,"DECIDE",&decide);CHKERRQ(ierr);
122   }
123 
124   if (tdefault) {
125     *a = PETSC_DEFAULT;
126   } else if (decide) {
127     *a = PETSC_DECIDE;
128   } else {
129     if (name[0] != '+' && name[0] != '-' && name[0] != '.' && name[0] < '0' && name[0] > '9') {
130       SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Input string %s has no numeric value ",name);
131     }
132     *a  = atof(name);
133   }
134   PetscFunctionReturn(0);
135 }
136 
137 #undef __FUNCT__
138 #define __FUNCT__ "PetscGetProgramName"
139 /*@C
140     PetscGetProgramName - Gets the name of the running program.
141 
142     Not Collective
143 
144     Input Parameter:
145 .   len - length of the string name
146 
147     Output Parameter:
148 .   name - the name of the running program
149 
150    Level: advanced
151 
152     Notes:
153     The name of the program is copied into the user-provided character
154     array of length len.  On some machines the program name includes
155     its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN.
156 @*/
157 PetscErrorCode PETSC_DLLEXPORT PetscGetProgramName(char name[],size_t len)
158 {
159   PetscErrorCode ierr;
160 
161   PetscFunctionBegin;
162   if (!options) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Must call PetscInitialize() first");
163   if (!options->namegiven) SETERRQ(PETSC_ERR_PLIB,"Unable to determine program name");
164   ierr = PetscStrncpy(name,options->programname,len);CHKERRQ(ierr);
165   PetscFunctionReturn(0);
166 }
167 
168 #undef __FUNCT__
169 #define __FUNCT__ "PetscSetProgramName"
170 PetscErrorCode PETSC_DLLEXPORT PetscSetProgramName(const char name[])
171 {
172   PetscErrorCode ierr;
173 
174   PetscFunctionBegin;
175   options->namegiven = PETSC_TRUE;
176   ierr  = PetscStrncpy(options->programname,name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
177   PetscFunctionReturn(0);
178 }
179 
180 #undef __FUNCT__
181 #define __FUNCT__ "PetscOptionsValidKey"
182 PetscErrorCode PETSC_DLLEXPORT PetscOptionsValidKey(const char in_str[],PetscTruth *key)
183 {
184   PetscFunctionBegin;
185   *key = PETSC_FALSE;
186   if (!in_str) PetscFunctionReturn(0);
187   if (in_str[0] != '-') PetscFunctionReturn(0);
188   if ((in_str[1] < 'A') || (in_str[1] > 'z')) PetscFunctionReturn(0);
189   *key = PETSC_TRUE;
190   PetscFunctionReturn(0);
191 }
192 
193 #undef __FUNCT__
194 #define __FUNCT__ "PetscOptionsInsertString"
195 /*@C
196      PetscOptionsInsertString - Inserts options into the database from a string
197 
198      Not collective: but only processes that call this routine will set the options
199                      included in the file
200 
201   Input Parameter:
202 .   in_str - string that contains options separated by blanks
203 
204 
205   Level: intermediate
206 
207   Contributed by Boyana Norris
208 
209 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(),
210           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
211           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
212           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
213           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
214           PetscOptionsList(), PetscOptionsEList(), PetscOptionsInsertFile()
215 
216 @*/
217 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertString(const char in_str[])
218 {
219   char           *first,*second;
220   PetscErrorCode ierr;
221   PetscToken     *token;
222   PetscTruth     key;
223 
224   PetscFunctionBegin;
225   ierr = PetscTokenCreate(in_str,' ',&token);CHKERRQ(ierr);
226   ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
227   while (first) {
228     ierr = PetscOptionsValidKey(first,&key);CHKERRQ(ierr);
229     if (key) {
230       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
231       ierr = PetscOptionsValidKey(second,&key);CHKERRQ(ierr);
232       if (!key) {
233         ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr);
234         ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
235       } else {
236         ierr  = PetscOptionsSetValue(first,PETSC_NULL);CHKERRQ(ierr);
237         first = second;
238       }
239     } else {
240       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
241     }
242   }
243   ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
244   PetscFunctionReturn(0);
245 }
246 
247 #undef __FUNCT__
248 #define __FUNCT__ "PetscOptionsInsertFile"
249 /*@C
250      PetscOptionsInsertFile - Inserts options into the database from a file.
251 
252      Not collective: but only processes that call this routine will set the options
253                      included in the file
254 
255   Input Parameter:
256 .   file - name of file
257 
258 
259   Level: intermediate
260 
261 .seealso: PetscOptionsSetValue(), PetscOptionsPrint(), PetscOptionsHasName(), PetscOptionsGetInt(),
262           PetscOptionsGetReal(), PetscOptionsGetString(), PetscOptionsGetIntArray(), PetscOptionsTruth(),
263           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
264           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
265           PetscOptionsTruthGroupBegin(), PetscOptionsTruthGroup(), PetscOptionsTruthGroupEnd(),
266           PetscOptionsList(), PetscOptionsEList()
267 
268 @*/
269 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsertFile(const char file[])
270 {
271   char           string[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],*first,*second,*third,*final;
272   PetscErrorCode ierr;
273   size_t         i,len,startIndex;
274   FILE           *fd;
275   PetscToken     *token;
276 
277   PetscFunctionBegin;
278   ierr = PetscFixFilename(file,fname);CHKERRQ(ierr);
279   fd   = fopen(fname,"r");
280   if (fd) {
281     while (fgets(string,128,fd)) {
282       /* Comments are indicated by #, ! or % in the first column */
283       if (string[0] == '#') continue;
284       if (string[0] == '!') continue;
285       if (string[0] == '%') continue;
286 
287       ierr = PetscStrlen(string,&len);CHKERRQ(ierr);
288 
289       /* replace tabs, ^M with " " */
290       for (i=0; i<len; i++) {
291         if (string[i] == '\t' || string[i] == '\r') {
292           string[i] = ' ';
293         }
294       }
295       for(startIndex = 0; startIndex < len-1; startIndex++) {
296         if (string[startIndex] != ' ') break;
297       }
298       ierr = PetscTokenCreate(&string[startIndex],' ',&token);CHKERRQ(ierr);
299       ierr = PetscTokenFind(token,&first);CHKERRQ(ierr);
300       ierr = PetscTokenFind(token,&second);CHKERRQ(ierr);
301       if (first && first[0] == '-') {
302         if (second) {final = second;} else {final = first;}
303         ierr = PetscStrlen(final,&len);CHKERRQ(ierr);
304         while (len > 0 && (final[len-1] == ' ' || final[len-1] == '\n')) {
305           len--; final[len] = 0;
306         }
307         ierr = PetscOptionsSetValue(first,second);CHKERRQ(ierr);
308       } else if (first) {
309         PetscTruth match;
310 
311         ierr = PetscStrcasecmp(first,"alias",&match);CHKERRQ(ierr);
312         if (match) {
313           ierr = PetscTokenFind(token,&third);CHKERRQ(ierr);
314           if (!third) SETERRQ1(PETSC_ERR_ARG_WRONG,"Error in options file:alias missing (%s)",second);
315           ierr = PetscStrlen(third,&len);CHKERRQ(ierr);
316           if (third[len-1] == '\n') third[len-1] = 0;
317           ierr = PetscOptionsSetAlias(second,third);CHKERRQ(ierr);
318         }
319       }
320       ierr = PetscTokenDestroy(token);CHKERRQ(ierr);
321     }
322     fclose(fd);
323   } else {
324     SETERRQ1(PETSC_ERR_USER,"Unable to open Options File %s",fname);
325   }
326   PetscFunctionReturn(0);
327 }
328 
329 #undef __FUNCT__
330 #define __FUNCT__ "PetscOptionsInsert"
331 /*@C
332    PetscOptionsInsert - Inserts into the options database from the command line,
333                    the environmental variable and a file.
334 
335    Input Parameters:
336 +  argc - count of number of command line arguments
337 .  args - the command line arguments
338 -  file - optional filename, defaults to ~username/.petscrc
339 
340    Note:
341    Since PetscOptionsInsert() is automatically called by PetscInitialize(),
342    the user does not typically need to call this routine. PetscOptionsInsert()
343    can be called several times, adding additional entries into the database.
344 
345    Options Database Keys:
346 +   -options_monitor <optional filename> - print options names and values as they are set
347 
348    Level: advanced
349 
350    Concepts: options database^adding
351 
352 .seealso: PetscOptionsDestroy_Private(), PetscOptionsPrint(), PetscOptionsInsertString(), PetscOptionsInsertFile(),
353           PetscInitialize()
354 @*/
355 PetscErrorCode PETSC_DLLEXPORT PetscOptionsInsert(int *argc,char ***args,const char file[])
356 {
357   PetscErrorCode ierr;
358   PetscMPIInt    rank;
359   char           pfile[PETSC_MAX_PATH_LEN];
360   PetscTruth     flag;
361 
362   PetscFunctionBegin;
363   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
364 
365   options->argc     = (argc) ? *argc : 0;
366   options->args     = (args) ? *args : PETSC_NULL;
367 
368   if (file) {
369     ierr = PetscOptionsInsertFile(file);CHKERRQ(ierr);
370   }
371   ierr = PetscOptionsHasName(PETSC_NULL,"-skip_petscrc",&flag);CHKERRQ(ierr);
372   if (!flag) {
373     ierr = PetscGetHomeDirectory(pfile,PETSC_MAX_PATH_LEN-16);CHKERRQ(ierr);
374     if (pfile[0]) {
375       ierr = PetscStrcat(pfile,"/.petscrc");CHKERRQ(ierr);
376       ierr = PetscTestFile(pfile,'r',&flag);CHKERRQ(ierr);
377       if (flag) {
378 	ierr = PetscOptionsInsertFile(pfile);CHKERRQ(ierr);
379 	ierr = PetscInfo(0,"Loading ~/.petscrc\n");CHKERRQ(ierr);
380       }
381     }
382     ierr = PetscTestFile(".petscrc",'r',&flag);CHKERRQ(ierr);
383     if (flag) {
384       ierr = PetscOptionsInsertFile(".petscrc");CHKERRQ(ierr);
385       ierr = PetscInfo(0,"Loading local directory file .petscrc\n");CHKERRQ(ierr);
386     }
387   }
388 
389   /* insert environmental options */
390   {
391     char   *eoptions = 0;
392     size_t len = 0;
393     if (!rank) {
394       eoptions = (char*)getenv("PETSC_OPTIONS");
395       ierr     = PetscStrlen(eoptions,&len);CHKERRQ(ierr);
396       ierr     = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
397     } else {
398       ierr = MPI_Bcast(&len,1,MPI_INT,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
399       if (len) {
400         ierr = PetscMalloc((len+1)*sizeof(char*),&eoptions);CHKERRQ(ierr);
401       }
402     }
403     if (len) {
404       ierr          = MPI_Bcast(eoptions,len,MPI_CHAR,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
405       if (rank) eoptions[len] = 0;
406       ierr = PetscOptionsInsertString(eoptions);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   PetscFunctionReturn(0);
455 }
456 
457 #undef __FUNCT__
458 #define __FUNCT__ "PetscOptionsPrint"
459 /*@C
460    PetscOptionsPrint - Prints the options that have been loaded. This is
461    useful for debugging purposes.
462 
463    Collective on PETSC_COMM_WORLD
464 
465    Input Parameter:
466 .  FILE fd - location to print options (usually stdout or stderr)
467 
468    Options Database Key:
469 .  -optionstable - Activates PetscOptionsPrint() within PetscFinalize()
470 
471    Level: advanced
472 
473    Concepts: options database^printing
474 
475 .seealso: PetscOptionsAllUsed()
476 @*/
477 PetscErrorCode PETSC_DLLEXPORT PetscOptionsPrint(FILE *fd)
478 {
479   PetscErrorCode ierr;
480   PetscInt       i;
481 
482   PetscFunctionBegin;
483   if (!fd) fd = PETSC_STDOUT;
484   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
485   for (i=0; i<options->N; i++) {
486     if (options->values[i]) {
487       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s %s\n",options->names[i],options->values[i]);CHKERRQ(ierr);
488     } else {
489       ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"OptionTable: -%s\n",options->names[i]);CHKERRQ(ierr);
490     }
491   }
492   PetscFunctionReturn(0);
493 }
494 
495 #undef __FUNCT__
496 #define __FUNCT__ "PetscOptionsGetAll"
497 /*@C
498    PetscOptionsGetAll - Lists all the options the program was run with in a single string.
499 
500    Not Collective
501 
502    Output Parameter:
503 .  copts - pointer where string pointer is stored
504 
505    Level: advanced
506 
507    Concepts: options database^listing
508 
509 .seealso: PetscOptionsAllUsed(), PetscOptionsPrint()
510 @*/
511 PetscErrorCode PETSC_DLLEXPORT PetscOptionsGetAll(char *copts[])
512 {
513   PetscErrorCode ierr;
514   PetscInt       i;
515   size_t         len = 1,lent;
516   char           *coptions;
517 
518   PetscFunctionBegin;
519   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
520 
521   /* count the length of the required string */
522   for (i=0; i<options->N; i++) {
523     ierr = PetscStrlen(options->names[i],&lent);CHKERRQ(ierr);
524     len += 2 + lent;
525     if (options->values[i]) {
526       ierr = PetscStrlen(options->values[i],&lent);CHKERRQ(ierr);
527       len += 1 + lent;
528     }
529   }
530   ierr = PetscMalloc(len*sizeof(char),&coptions);CHKERRQ(ierr);
531   coptions[0] = 0;
532   for (i=0; i<options->N; i++) {
533     ierr = PetscStrcat(coptions,"-");CHKERRQ(ierr);
534     ierr = PetscStrcat(coptions,options->names[i]);CHKERRQ(ierr);
535     ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
536     if (options->values[i]) {
537       ierr = PetscStrcat(coptions,options->values[i]);CHKERRQ(ierr);
538       ierr = PetscStrcat(coptions," ");CHKERRQ(ierr);
539     }
540   }
541   *copts = coptions;
542   PetscFunctionReturn(0);
543 }
544 
545 #undef __FUNCT__
546 #define __FUNCT__ "PetscOptionsDestroy"
547 /*@C
548     PetscOptionsDestroy - Destroys the option database.
549 
550     Note:
551     Since PetscOptionsDestroy() is called by PetscFinalize(), the user
552     typically does not need to call this routine.
553 
554    Level: developer
555 
556 .seealso: PetscOptionsInsert()
557 @*/
558 PetscErrorCode PETSC_DLLEXPORT PetscOptionsDestroy(void)
559 {
560   PetscInt i;
561 
562   PetscFunctionBegin;
563   if (!options) PetscFunctionReturn(0);
564   for (i=0; i<options->N; i++) {
565     if (options->names[i]) free(options->names[i]);
566     if (options->values[i]) free(options->values[i]);
567   }
568   for (i=0; i<options->Naliases; i++) {
569     free(options->aliases1[i]);
570     free(options->aliases2[i]);
571   }
572   free(options);
573   options = 0;
574   PetscFunctionReturn(0);
575 }
576 
577 #undef __FUNCT__
578 #define __FUNCT__ "PetscOptionsSetValue"
579 /*@C
580    PetscOptionsSetValue - Sets an option name-value pair in the options
581    database, overriding whatever is already present.
582 
583    Not collective, but setting values on certain processors could cause problems
584    for parallel objects looking for options.
585 
586    Input Parameters:
587 +  name - name of option, this SHOULD have the - prepended
588 -  value - the option value (not used for all options)
589 
590    Level: intermediate
591 
592    Note:
593    Only some options have values associated with them, such as
594    -ksp_rtol tol.  Other options stand alone, such as -ksp_monitor.
595 
596   Concepts: options database^adding option
597 
598 .seealso: PetscOptionsInsert()
599 @*/
600 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetValue(const char iname[],const char value[])
601 {
602   size_t         len;
603   PetscErrorCode ierr;
604   PetscInt       N,n,i;
605   char           **names;
606   const char     *name = (char*)iname;
607   PetscTruth     gt,match;
608 
609   PetscFunctionBegin;
610   if (!options) {ierr = PetscOptionsInsert(0,0,0);CHKERRQ(ierr);}
611 
612   /* this is so that -h and -help are equivalent (p4 does not like -help)*/
613   ierr = PetscStrcasecmp(name,"-h",&match);CHKERRQ(ierr);
614   if (match) name = "-help";
615 
616   name++;
617   /* first check against aliases */
618   N = options->Naliases;
619   for (i=0; i<N; i++) {
620     ierr = PetscStrcasecmp(options->aliases1[i],name,&match);CHKERRQ(ierr);
621     if (match) {
622       name = options->aliases2[i];
623       break;
624     }
625   }
626 
627   N     = options->N;
628   n     = N;
629   names = options->names;
630 
631   for (i=0; i<N; i++) {
632     ierr = PetscStrcasecmp(names[i],name,&match);CHKERRQ(ierr);
633     ierr  = PetscStrgrt(names[i],name,&gt);CHKERRQ(ierr);
634     if (match) {
635       if (options->values[i]) free(options->values[i]);
636       ierr = PetscStrlen(value,&len);CHKERRQ(ierr);
637       if (len) {
638         options->values[i] = (char*)malloc((len+1)*sizeof(char));
639         ierr = PetscStrcpy(options->values[i],value);CHKERRQ(ierr);
640       } else { options->values[i] = 0;}
641       PetscOptionsMonitor(name,value);
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<(int)len; i++) {
1407       if (value[i] == '-') {
1408         if (i == (int)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   PetscOptionsObject.prefix = PETSC_NULL;
1641   PetscOptionsObject.title  = PETSC_NULL;
1642 
1643   PetscFunctionReturn(0);
1644 }
1645 
1646 #undef __FUNCT__
1647 #define __FUNCT__ "PetscOptionsSetFromOptions"
1648 /*@
1649    PetscOptionsSetFromOptions - Sets various SNES and KSP parameters from user options.
1650 
1651    Collective on PETSC_COMM_WORLD
1652 
1653    Options Database Keys:
1654 +  -options_monitor <optional filename> - prints the names and values of all
1655  				runtime options as they are set. The monitor functionality is not
1656                 available for options set through a file, environment variable, or on
1657                 the command line. Only options set after PetscInitialize completes will
1658                 be monitored.
1659 .  -options_monitor_cancel - cancel all options database monitors
1660 
1661    Notes:
1662    To see all options, run your program with the -help option or consult
1663    the users manual.
1664 
1665    Level: intermediate
1666 
1667 .keywords: set, options, database
1668 @*/
1669 PetscErrorCode PETSC_DLLEXPORT PetscOptionsSetFromOptions(void)
1670 {
1671   PetscTruth          flg;
1672   PetscErrorCode      ierr;
1673   char                monfilename[PETSC_MAX_PATH_LEN];
1674   PetscViewer         monviewer;
1675 
1676   PetscFunctionBegin;
1677 
1678   ierr = PetscOptionsBegin(PETSC_COMM_WORLD,"","Options database options","PetscOptions");CHKERRQ(ierr);
1679   ierr = PetscOptionsString("-options_monitor","Monitor options database","PetscOptionsMonitorSet","stdout",monfilename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
1680   if (flg && (!options->numbermonitors)) {
1681     ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,monfilename,&monviewer);CHKERRQ(ierr);
1682     ierr = PetscOptionsMonitorSet(PetscOptionsMonitorDefault,monviewer,(PetscErrorCode (*)(void*))PetscViewerDestroy);CHKERRQ(ierr);
1683   }
1684 
1685   ierr = PetscOptionsName("-options_monitor_cancel","Cancel all options database monitors","PetscOptionsMonitorCancel",&flg);CHKERRQ(ierr);
1686   if (flg) { ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr); }
1687 
1688   ierr = PetscOptionsEnd();CHKERRQ(ierr);
1689 
1690   PetscFunctionReturn(0);
1691 }
1692 
1693 
1694 #undef __FUNCT__
1695 #define __FUNCT__ "PetscOptionsMonitorDefault"
1696 /*@C
1697    PetscOptionsMonitorDefault - Print all options set value events.
1698 
1699    Collective on PETSC_COMM_WORLD
1700 
1701    Input Parameters:
1702 +  name  - option name string
1703 .  value - option value string
1704 -  dummy - unused monitor context
1705 
1706    Level: intermediate
1707 
1708 .keywords: PetscOptions, default, monitor
1709 
1710 .seealso: PetscOptionsMonitorSet()
1711 @*/
1712 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorDefault(const char name[], const char value[], void *dummy)
1713 {
1714   PetscErrorCode ierr;
1715   PetscViewer    viewer = (PetscViewer) dummy;
1716 
1717   PetscFunctionBegin;
1718   if (!viewer) {
1719     ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1720   }
1721   ierr = PetscViewerASCIIPrintf(viewer,"Setting option: %s = %s\n",name,value);CHKERRQ(ierr);
1722   PetscFunctionReturn(0);
1723 }
1724 
1725 #undef __FUNCT__
1726 #define __FUNCT__ "PetscOptionsMonitorSet"
1727 /*@C
1728    PetscOptionsMonitorSet - Sets an ADDITIONAL function to be called at every method that
1729    modified the PETSc options database.
1730 
1731    Not collective
1732 
1733    Input Parameters:
1734 +  monitor - pointer to function (if this is PETSC_NULL, it turns off monitoring
1735 .  mctx    - [optional] context for private data for the
1736              monitor routine (use PETSC_NULL if no context is desired)
1737 -  monitordestroy - [optional] routine that frees monitor context
1738           (may be PETSC_NULL)
1739 
1740    Calling Sequence of monitor:
1741 $     monitor (const char name[], const char value[], void *mctx)
1742 
1743 +  name - option name string
1744 .  value - option value string
1745 -  mctx  - optional monitoring context, as set by PetscOptionsMonitorSet()
1746 
1747    Options Database Keys:
1748 +    -options_monitor    - sets PetscOptionsMonitorDefault()
1749 -    -options_monitor_cancel - cancels all monitors that have
1750                           been hardwired into a code by
1751                           calls to PetscOptionsMonitorSet(), but
1752                           does not cancel those set via
1753                           the options database.
1754 
1755    Notes:
1756    The default is to do nothing.  To print the name and value of options
1757    being inserted into the database, use PetscOptionsMonitorDefault() as the monitoring routine,
1758    with a null monitoring context.
1759 
1760    Several different monitoring routines may be set by calling
1761    PetscOptionsMonitorSet() multiple times; all will be called in the
1762    order in which they were set.
1763 
1764    Level: beginner
1765 
1766 .keywords: PetscOptions, set, monitor
1767 
1768 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorCancel()
1769 @*/
1770 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorSet(PetscErrorCode (*monitor)(const char name[], const char value[], void*),void *mctx,PetscErrorCode (*monitordestroy)(void*))
1771 {
1772   PetscFunctionBegin;
1773   if (options->numbermonitors >= MAXOPTIONSMONITORS) {
1774     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptions monitors set");
1775   }
1776   options->monitor[options->numbermonitors]           = monitor;
1777   options->monitordestroy[options->numbermonitors]    = monitordestroy;
1778   options->monitorcontext[options->numbermonitors++]  = (void*)mctx;
1779   PetscFunctionReturn(0);
1780 }
1781 
1782 #undef __FUNCT__
1783 #define __FUNCT__ "PetscOptionsMonitorCancel"
1784 /*@
1785    PetscOptionsMonitorCancel - Clears all monitors for a PetscOptions object.
1786 
1787    Not collective
1788 
1789    Options Database Key:
1790 .  -options_monitor_cancel - Cancels all monitors that have
1791     been hardwired into a code by calls to PetscOptionsMonitorSet(),
1792     but does not cancel those set via the options database.
1793 
1794    Level: intermediate
1795 
1796 .keywords: PetscOptions, set, monitor
1797 
1798 .seealso: PetscOptionsMonitorDefault(), PetscOptionsMonitorSet()
1799 @*/
1800 PetscErrorCode PETSC_DLLEXPORT PetscOptionsMonitorCancel(void)
1801 {
1802   PetscErrorCode ierr;
1803   PetscInt       i;
1804 
1805   PetscFunctionBegin;
1806   for (i=0; i<options->numbermonitors; i++) {
1807     if (options->monitordestroy[i]) {
1808       ierr = (*options->monitordestroy[i])(options->monitorcontext[i]);CHKERRQ(ierr);
1809     }
1810   }
1811   options->numbermonitors = 0;
1812   PetscFunctionReturn(0);
1813 }
1814